如何:在 Visual Basic 中重命名文件

使用 My.Computer.FileSystem 对象的 RenameFile 方法可通过提供当前位置、文件名和新文件名来重命名文件。 此方法不能用于移动文件;使用 MoveFile 方法可移动并重命名文件。

重命名文件

  • 使用 My.Computer.FileSystem.RenameFile 方法可重命名文件。 此示例将名为 Test.txt 的文件重命名为 SecondTest.txt

    ' Change "c:\test.txt" to the path and filename for the file that
    ' you want to rename.
    My.Computer.FileSystem.RenameFile("C:\Test.txt", "SecondTest.txt")
    

此代码示例也可作为 IntelliSense 代码片段。 在代码片段选取器中,该代码段位于“文件系统 - 处理驱动器、文件夹和文件”。 有关详细信息,请参阅代码片段

可靠编程

以下情况可能会导致异常:

另请参阅