增加文件名时出现问题

时间:2011-06-07 17:48:28

标签: .net vb.net

  

可能重复:
  Way to get unique filename if specified filename already exists (.NET)

查看以下代码:

Dim counter As Integer = 0
While System.IO.File.Exists("C:\Desktop\Sample.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).xls

如何避免这个问题?有什么建议吗?

5 个答案:

答案 0 :(得分:1)

在第一遍,在这一行:

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

newfile将为sample(1).xls。然后用

格式化它
System.IO.Path.GetFileNameWithoutExtension(newfile)

它将在格式字符串中返回sample(1){1},然后在您添加({2})的循环的第二遍中,2,这使{ {1}}。

要解决此问题,您需要不要继续将(x)添加到文件名中,如下所示:

sample(1)(2)

您的原始代码中存在另一个问题

        Dim counter As Integer = 0
        Dim origfile As String = "C:\Desktop\Sample.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:\Desktop\Sample.xls") ,则会导致无限循环。

答案 1 :(得分:1)

试试这个

Dim counter As Integer = 0
Dim tempBaseFile As String = "C:\Desktop\Sample.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

答案 2 :(得分:0)

请试试这个

        Dim counter As Integer = 0
        While System.IO.File.Exists("C:\Desktop\Sample.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

原因是因为你需要在没有括号(###)

的内容的情况下获取filenamewithoutextension

答案 3 :(得分:0)

逻辑可以是:

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)

答案 4 :(得分:0)

你也可以这样做:

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