My.Computer.FileSystem.MoveFile Method
Moves a file to a new location.
' Usage
My.Computer.FileSystem.MoveFile(sourceFileName ,destinationFileName)
My.Computer.FileSystem.MoveFile(sourceFileName ,destinationFileName ,overwrite)
My.Computer.FileSystem.MoveFile(sourceFileName ,destinationFileName ,showUI)
My.Computer.FileSystem.MoveFile(sourceFileName ,destinationFileName ,showUI ,onUserCancel)
' Declaration
Public Sub MoveFile( _
ByVal sourceFileName As String, _
ByVal destinationFileName As String _
)
' -or-
Public Sub MoveFile( _
ByVal sourceFileName As String, _
ByVal destinationFileName As String, _
ByVal overwrite As Boolean _
)
' -or-
Public Sub MoveFile( _
ByVal sourceFileName As String, _
ByVal destinationFileName As String, _
ByVal showUI As UIOption _
)
' -or-
Public Sub MoveFile( _
ByVal sourceFileName As String, _
ByVal destinationFileName As String, _
ByVal showUI As UIOption, _
ByVal onUserCancel As UICancelOption _
)
Parameters
sourceFileName
String. Path of the file to be moved. Required.destinationFileName
String. Path of the directory into which the file should be moved. Required.overwrite
Boolean. Specifies whether to overwrite existing files. Default is False. Required.showUI
UIOption Enumeration. Specifies whether to visually track the operation's progress. Default is UIOption.OnlyErrorDialogs. Required.onUserCancel
UICancelOption Enumeration. Specifies whether or not an exception is thrown when the user cancels the operation. Default is UICancelOption.ThrowException. Required.
Exceptions
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 file name ends with a backslash (\) (ArgumentException).
The path is not valid because it is Nothing (ArgumentNullException).
destinationFileName is Nothing or an empty string (ArgumentNullException).
The source file is not valid or does not exist (FileNotFoundException).
The combined path points to an existing directory, the destination file exists and overwrite is set to False, a file in the target directory with the same name is in use, orthe user does not have sufficient permissions to access the file (IOException).
A file or directory name in the path contains a colon (:) or is in an invalid format (NotSupportedException).
onUserCancel is set to ThrowException, and either the user has cancelled the operation or an unspecified I/O error occurs (OperationCanceledException).
The path exceeds the system-defined maximum length (PathTooLongException).
The user lacks necessary permissions to view the path (SecurityException).
The user does not have required permission (UnauthorizedAccessException).
Remarks
If the target structure does not exist, it is created.
The MoveFile method preserves ACEs (Access Control Entries) only when moving the file within the same volume. This includes inherited ACEs, which become direct ACEs when moved (direct ACEs take precedence over inherited ACEs). If a file is moved between volumes, ACEs will not be copied.
Tasks
The following table lists examples of tasks involving the My.Computer.FileSystem.MoveFile method.
To |
See |
---|---|
Move a file |
|
Move a collection of files |
|
Move the contents of a directory |
Example
This example moves the file Test.txt from TestDir1 to TestDir2.
My.Computer.FileSystem.MoveFile("C:\TestDir1\test.txt", "C:\TestDir2\test.txt")
This example moves the file Test.txt from TestDir1 to TestDir2 and renames it Test2.txt.
My.Computer.FileSystem.MoveFile("C:\TestDir1\test.txt", "C:\TestDir2\test2.txt")
Requirements
Namespace:Microsoft.VisualBasic.MyServices
Class:FileSystemProxy (provides access to FileSystem)
Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)
Availability by Project Type
Project type |
Available |
---|---|
Windows Application |
Yes |
Class Library |
Yes |
Console Application |
Yes |
Windows Control Library |
Yes |
Web Control Library |
Yes |
Windows Service |
Yes |
Web Site |
Yes |
Permissions
The following permissions may be necessary:
Permission |
Description |
---|---|
Controls the ability to access all environment variables. Associated enumeration: Unrestricted. |
|
Controls the ability to access files and folders. Associated enumeration: Unrestricted. |
|
Controls the ability to access registry variables. Associated enumeration: Unrestricted. |
|
Controls the permissions related to user interfaces and the clipboard. Associated enumeration: SafeSubWindows. |
For more information, see Code Access Security and Requesting Permissions.
See Also
Reference
Other Resources
Creating, Deleting, and Moving Files and Directories in Visual Basic