可能重複:
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
。
如何避免此問題?有什麼建議麼?
可能dublicate:http://stackoverflow.com/questions/ 3093008 /方式對獲得唯一的文件名,如果指定的文件名,已經存在的網 – Stefan 2011-06-07 17:52:31