English 中文(简体)
增收档案名称的问题[重复]
原标题:Problem in incrementing the filename [duplicate]
  • 时间:2011-06-07 17:48:28
  •  标签:
  • .net
  • vb.net
This question already has answers here:
Closed 12 years ago.

Possible Duplicate:
Way to get unique filename if specified filename already exists (.NET)

研究了以下法典:

Dim counter As Integer = 0
While System.IO.File.Exists("C:DesktopSample.xls")
    counter = counter + 1
    Dim fileName As String = String.Format("{0}({1}){2}",    System.IO.Path.GetFileNameWithoutExtension(newfile), counter.ToString(), System.IO.Path.GetExtension(newfile))
    newfile = System.IO.Path.Combine(ProcessedView.processedPath, fileName)
End While

如果档案存在,新的档案名称将是Sample(1).xls。 直到这一点为止,它一直在做罚款。 如果档案名称本身为Sample(1).xls,则新的档案名称应为Sample(2).xls。 但是,在这项法典中,我把它作为<条码>Sample(1)(2)。

How to avoid this problem? Any suggestions?

问题回答

On the first pass, in this line:

newfile = System.IO.Path.Combine(ProcessedView.processedPath, fileName)

页: 1 然后,由你来安排。

System.IO.Path.GetFileNameWithoutExtension(newfile)

填表格式的<代码>sample(1)>>,<1},然后在座右边的第二份通行证中加入<代码>({2}),即<代码>2,并填写sample(1)(2)>。

为了确定这一点,你不必把(x)添加到档案名称上,例如:

        Dim counter As Integer = 0
        Dim origfile As String = "C:DesktopSample.xls"
        Dim newfile As String = origfile
        While System.IO.File.Exists(newfile)
            counter = counter + 1
            newfile = String.Format("{0}({1}){2}", System.IO.Path.GetFileNameWithoutExtension(newfile), counter.ToString(), System.IO.Path.GetExtension(newfile))
        End While

        use newfile instead of origfile

你最初的法典中还有另一个问题。

While System.IO.File.Exists("C:DesktopSample.xls")

因为如果存在<代码>C:Desktop Sample.xls,就会造成一种无限的漏洞。

引言

Dim counter As Integer = 0
Dim tempBaseFile As String = "C:DesktopSample.xls"
Dim newFile As String = tempBaseFile

While System.IO.File.Exists(newFile)
  counter = counter + 1
  Dim fileName As String = String.Format("{0}({1}){2}", System.IO.Path.GetFileNameWithoutExtension(tempBaseFile), counter.ToString(), System.IO.Path.GetExtension(newFile))
  newFile = System.IO.Path.Combine(ProcessedView.processedPath, fileName)
End While

请审判

        Dim counter As Integer = 0
        While System.IO.File.Exists("C:DesktopSample.xls")
            counter = counter + 1
            Dim fileName As String = String.Format("{0}({1}){2}",    System.IO.Path.GetFileNameWithoutExtension(Mid(newfile, 1, InStr(newfile, "(") - 1)), counter.ToString(), System.IO.Path.GetExtension(newfile))
            newfile = System.IO.Path.Combine(ProcessedView.processedPath, fileName)
        End While

原因是,你需要获得姓名,但不必再附上括号中的缺陷:(##)

逻辑可以是:

Dim counter As Integer = 0 
Dim testFileName as String = "Sample"
Dim searchFileName as String = testFileName
While System.IO.File.Exists("C:Desktop" & searchFileName & ".xls")
   counter = counter + 1
   searchFileName  = testFileName & "(" & counter & ")"
End While

Dim fileName As String = String.Format("{0}({1}){2}",           System.IO.Path.GetFileNameWithoutExtension(newfile), counter.ToString(), System.IO.Path.GetExtension(newfile))
newfile = System.IO.Path.Combine(ProcessedView.processedPath, fileName)

也可以这样做:

Dim counter As Integer = 0

Dim newFileName As String = orginialFileName

While File.Exists(newFileName)
    counter = counter + 1
    newFileName = String.Format("{0}({1}", orginialFileName, counter.ToString())
End While




相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签