2012-09-20 45 views
0

我需要在每次上傳文件時在文件末尾添加增量編號。我去做它幾乎工作。爲上傳文件添加增量編號

請檢查下面的代碼:

Dim intVersion As Integer = 1 
    While (System.IO.File.Exists(strDestinationPath)) 
     Dim temp As Integer = ourFilename.LastIndexOf(".") 
     Dim temp2 As String = ourFilename.Substring(temp) 
     ourFilename = ourFilename.Replace(temp2, "_" & intVersion & temp2) 
     strDestinationPath = System.Configuration.ConfigurationManager.AppSettings("WebLocalContentDir") & "\VisualID\" & ourFilename 
     intVersion += 1 
    End While 

如果我上傳的文件3倍,其另存爲,

第1次:VDFGH 第2次:VDFGH_1 第3次:VDFGH_1_2(預期輸出是VDFGH_2)

回答

1
Dim intVersion As Integer = 1 
While (System.IO.File.Exists(strDestinationPath)) 
    Dim temp As Integer = ourFilename.LastIndexOf(".") 
    Dim temp2 As String = ourFilename.Substring(temp) 
    dim tempFileName as string = ourFilename.Replace(temp2, "_" & intVersion & temp2) 
    strDestinationPath = System.Configuration.ConfigurationManager.AppSettings("WebLocalContentDir") & "\VisualID\" & tempFileName 
    intVersion += 1 
End While 

很確定這會起作用。您正在更改您的原始文件名字符串,然後每次修改它。

+0

感謝它的工作... – Joshua