Renames a file.
' Usage My.Computer.FileSystem.RenameFile(file ,newName) ' Declaration Public Sub RenameFile( _ ByVal file As String, _ ByVal newName As String _ )
String. File to be renamed. Required.
String. New name of file. Required.
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).
The newName parameter contains path information or ends with a backslash (\) (ArgumentException).
The path is not valid because it is Nothing (ArgumentNullException).
The newName parameter is Nothing or 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 newName (IOException).
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).
This method cannot be used to move a file; use the My.Computer.FileSystem.MoveFile Method to move and rename the file.
The following table lists an example of a task involving the My.Computer.FileSystem.RenameFile method.
To
See
Rename a file
How to: Rename a File in Visual Basic
This example renames the file Test.txt to SecondTest.txt.
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.
Namespace: Microsoft.VisualBasic.MyServices
Class: FileSystemProxy (provides access to FileSystem)
Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)
Project type
Available
Windows Application
Yes
Class Library
Console Application
Windows Control Library
Web Control Library
Windows Service
Web Site
The following permission may be necessary:
Permission
Description
FileIOPermission
Controls the ability to access files and folders. Associated enumeration: Unrestricted.
For more information, see Code Access Security and Requesting Permissions.