.net 递增文件名时出现问题

.net 递增文件名时出现问题,.net,vb.net,.net,Vb.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.ToStri

可能重复:

请查看以下代码:

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
。但在这段代码中,我得到的是
示例(1)(2).xls

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

请试试这个

        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

原因是您需要在第一次传递时获取不带扩展名的文件名,而不包含括号中的内容
(####)

,在这一行:

newfile = System.IO.Path.Combine(ProcessedView.processedPath, fileName)
newfile
将是
示例(1).xls
。然后当你用

System.IO.Path.GetFileNameWithoutExtension(newfile)
它将返回格式字符串中的
sample(1)
,它是
{1}
,然后在循环的第二个过程中添加
({2})
,它是
2
,这就使得
sample(1)(2)

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

        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")
因为如果
C:\Desktop\Sample.xls
存在,它将导致无限循环。

逻辑可以是:

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
试试这个

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
可能的副本: