How to: Rename a File in Visual Basic
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
My.Computer.FileSystem.RenameFilemethod to rename a file. This example renames the file named
' 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.
The following conditions may cause an exception:
The path is not valid for one of the following reasons: it is a zero-length string, it contains only white space, it contains invalid characters, or it is a device path (starts with \\.\) (ArgumentException).
newNamecontains path information (ArgumentException).
The path is not valid because it is
Nothingor an empty string (ArgumentNullException).
The source file is not valid or does not exist (FileNotFoundException).
There is an existing file or directory with the name specified in
The path exceeds the system-defined maximum length (PathTooLongException).
A file or directory name in the path contains a colon (:) or is in an invalid format (NotSupportedException).
The user lacks necessary permissions to view the path (SecurityException).
The user does not have the required permission (UnauthorizedAccessException).