为上传的文件添加增量编号

时间:2012-09-20 15:54:52

标签: asp.net vb.net file-upload

每次上传文件时,我都需要在文件末尾添加一个增量编号。我差不多工作了。

请检查以下代码:

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次,则保存为,

第一次:VDFGH 第二次:VDFGH_1 第3次:VDFGH_1_2(预期输出为VDFGH_2)

1 个答案:

答案 0 :(得分: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

很确定这会奏效。您正在更改原始文件名字符串,然后每次都进行修改。