VB存在文件时,更改FileName

时间:2013-01-02 20:00:07

标签: visual-studio-2010 file-io

只是寻求一些帮助。我尝试了一些不同的东西,我很难过。我正在尝试检查目录中是否存在文件,如果存在,则我想重命名添加- Copy到文件名。然后它应该再次检查是否存在冲突,如果不存在则应该移动文件。听起来很简单,但根本不起作用。由于我无法从String转换为DirectoryInfo,因此我必须声明多个变量,而且感觉不对。我该怎么做才能解决这个问题?

Dim fileExt As String = ""

Dim oldFileName As String = file.FullName
Dim newFileName As String = oldFileName
Dim newFileLocation = Environment.GetSpecialFolder(Environment.SpecialFolder.MyPictures) + "\" + file.Name

While FileIO.FileSystem.FileExists(newFileLocation) 'While File exists in new directory
    'Add copy to filename
    fileExt = fileType.Replace("*", "")
    newFileName = newFileName.Remove(newFileName.LastIndexOf("."), (newFileName.Length - newFileName.LastIndexOf(".")))
    newFileName += " - Copy"
    newFileName += fileExt

    'Rename file
    FileSystem.Rename(oldFileName, newFileName)

    'Declare a new DirInf variable because I can't use a string to set one
    Dim newFile As New DirectoryInfo(newFileName)

    'Move the new file to 
    newFile.MoveTo("C:\Users\" + Environ("USERNAME") + "\Pictures\")
    ProgressBar.Value += 1
End While

1 个答案:

答案 0 :(得分:1)

您正在寻找File.Move(),它需要两个字符串。

另外,"C:\Users\" + Environ("USERNAME") + "\Pictures\"是非常错误的;许多用户没有C:个驱动器 你应该致电Environment.GetSpecialFolder(Environment.SpecialFolder.MyPictures)