This documentation is archived and is not being maintained.

How to: Rename a Directory in Visual Basic 

The RenameDirectory method of the My.Computer.FileSystem object can be used to rename a directory by supplying the current location and name of the directory, along with the new name for the directory. This method cannot be used to move a directory; use the MoveDirectory method to move and rename the directory.

To rename a directory

  • Use the My.Computer.FileSystem.RenameDirectory method to rename a directory. The following code renames the Test directory to SecondTest.

    My.Computer.FileSystem.RenameDirectory("C:MyDocuments\Test", _

This code example is also available as an IntelliSense code snippet. In the code snippet picker, the example is located in File system - Processing Drives, Folders, and Files. For more information, see How to: Insert Snippets Into Your Code (Visual Basic).

Robust Programming

The following conditions can 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).

  • newName contains path information (ArgumentException).

  • The path is not valid because it is Nothing (ArgumentNullException).

  • newName is Nothing or an empty string (ArgumentNullException).

  • The source directory is not valid or does not exist (DirectoryNotFoundException).

  • There is an existing file or folder with the name specified in newName (IOException).

  • The folder is a root folder (IOException).

  • The path exceeds the system-defined maximum length (PathTooLongException).

  • A file or folder 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 required permission (UnauthorizedAccessException).

See Also