共用方式為


How to: Rename a File in Visual Basic

Use the RenameFile method of the My.Computer.FileSystem object to rename a file by supplying the current location, file name, and the new file name. This method cannot be used to move a file; use the MoveFile method to move and rename the file.

To rename a file

  • Use the My.Computer.FileSystem.RenameFile method to rename a file. This example renames the file named Test.txt to 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")
    

This code example is also available as an IntelliSense code snippet. In the code snippet picker, the snippet is located in File system - Processing Drives, Folders, and Files. For more information, see Code Snippets.

Robust Programming

The following conditions may cause an exception:

See Also

Tasks

How to: Move a File in Visual Basic

How to: Create a Copy of a File in the Same Directory in Visual Basic

How to: Create a Copy of a File in a Different Directory in Visual Basic

Reference

RenameFile

Other Resources

Creating, Deleting, and Moving Files and Directories in Visual Basic