Export (0) Print
Expand All

FileSystem.Rename Method

Renames a disk file or directory. The My feature gives you better productivity and performance in file I/O operations than Rename. For more information, see FileSystem.

Namespace:  Microsoft.VisualBasic
Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)

public static void Rename(
	string OldPath,
	string NewPath
)

Parameters

OldPath
Type: System.String

Required. String expression that specifies the existing file name and location. OldPath may include the directory, and drive, of the file.

NewPath
Type: System.String

Required. String expression that specifies the new file name and location. NewPath may include directory and drive of the destination location. The file name specified by NewPath cannot already exist.

ExceptionCondition
ArgumentException

Path is invalid.

FileNotFoundException

OldPath file does not exist.

IOException

NewPath file already exists.

IOException

Access is invalid.

IOException

Cannot rename to different device.

The Rename function renames a file and moves it to a different directory, if it is required. The Rename function can move a file across drives, but it can only rename an existing directory when both NewPath and OldPath are located on the same drive. Rename cannot create a new file or directory.

Using the Rename function on an open file produces an error. You must close an open file before renaming it. Rename arguments cannot include multiple-character (*) and single-character (?) wildcards.

Security noteSecurity Note

When using Rename to copy a file from an unprotected location to a protected location, the file retains the less restricted rights. Check to make sure that you are not introducing a possible security risk.

This example uses the Rename function to rename a file. For purposes of this example, assume that the directories that are specified already exist.

Dim OldName, NewName As String
OldName = "OLDFILE" 
' Define file names.
NewName = "NEWFILE" 
' Rename file.
Rename(OldName, NewName)

OldName = "C:\OLDDIR\OLDFILE"
NewName = "C:\NEWDIR\NEWFILE" 
' Move and rename file.
Rename(OldName, NewName)

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft