FileSystem.RenameFile(String, String) Method

Definition

Renames a file.

C#
public static void RenameFile(string file, string newName);

Parameters

file
String

File to be renamed.

newName
String

New name of file.

Exceptions

newName contains path information or ends with a backslash (\).

file is Nothing.

-or-

newName is Nothing or an empty string.

The directory does not exist.

There is an existing file or directory with the name specified in newName.

The path exceeds the system-defined maximum length.

A file or directory name in the path contains a colon (:) or is in an invalid format.

The user lacks necessary permissions to view the path.

The user does not have required permission.

Examples

This example renames the file Test.txt to SecondTest.txt.

VB
My.Computer.FileSystem.RenameFile("C:\Test.txt", "SecondTest.txt")

Change "C:\Test.txt" to the path and file name of the file that you want to rename.

Remarks

This method cannot be used to move a file; use the MoveFile method to move and rename the file.

The following table lists an example of a task involving the My.Computer.FileSystem.RenameFile method.

Applies to

Product Versions
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1