My.Computer.FileSystem.CopyFile Method
Copies a file to a new location.
' Usage My.Computer.FileSystem.CopyFile(sourceFileName ,destinationFileName) My.Computer.FileSystem.CopyFile(sourceFileName ,destinationFileName ,overwrite) My.Computer.FileSystem.CopyFile(sourceFileName ,destinationFileName ,showUI) My.Computer.FileSystem.CopyFile(sourceFileName ,destinationFileName ,showUI ,onUserCancel) ' Declaration Public Sub CopyFile( _ ByVal sourceFileName As String, _ ByVal destinationFileName As String _ ) ' -or- Public Sub CopyFile( _ ByVal sourceFileName As String, _ ByVal destinationFileName As String, _ ByVal overwrite As Boolean _ ) ' -or- Public Sub CopyFile( _ ByVal sourceFileName As String, _ ByVal destinationFileName As String, _ ByVal showUI As UIOption _ ) ' -or- Public Sub CopyFile( _ ByVal sourceFileName As String, _ ByVal destinationFileName As String, _ ByVal showUI As UIOption, _ ByVal onUserCancel As UICancelOption _ )
The following conditions may cause an exception to be thrown:
-
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 system could not retrieve the absolute path (ArgumentException).
-
destinationFileName contains path information (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 (IOException).
-
The destination file exists and overwrite is set to False (IOException).
-
The user does not have sufficient permissions to access the file (IOException).
-
A file in the target directory with the same name is in use (IOException).
-
A file or directory name in the path contains a colon (:) or is in an invalid format (NotSupportedException).
-
UICancelOption is set to ThrowException, and the user has canceled the operation (OperationCanceledException).
-
UICancelOption is set to ThrowException, and an unspecified I/O error occurs (OperationCanceledException).
-
The path exceeds the system-defined maximum length (PathTooLongException).
-
The user does not have required permission (UnauthorizedAccessException).
-
The user lacks necessary permissions to view the path (SecurityException).
The following table lists examples of tasks involving the My.Computer.FileSystem.CopyFile method.
|
To |
See |
|---|---|
|
Copy a file to the same directory. |
How to: Create a Copy of a File in the Same Directory in Visual Basic |
|
Copy a file to a different directory. |
How to: Create a Copy of a File in a Different Directory in Visual Basic |
This example copies the file Test.txt to the directory TestFiles2 without overwriting existing files.
Replace the file paths with the paths you want to use in your code.
This example copies the file Test.txt to the directory TestFiles2 and renames it NewFile.txt.
Replace the file paths with the paths you want to use in your code.
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 |
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.
Example 2 has been updated for the next release of the .NET Framework 4.0 documentation, where this topic now resides.
- 5/6/2010
- DougRothaus
My.Computer.FileSystem.CopyFile _
("C:\UserFiles\TestFiles\test.txt", _
"C:\UserFiles\TestFiles2", "NewFile.txt", FileIO.UICancelOption.DoNothing)
There is not any overloading function can pass this type parameters. The example code will cause error [System.InvalidCastException] .
It should change to :
My.Computer.FileSystem.CopyFile _
("C:\UserFiles\TestFiles\test.txt", _
"C:\UserFiles\TestFiles2\NewFile.txt", FileIO.UIOption.OnlyErrorDialogs., FileIO.UICancelOption.DoNothing)
- 11/30/2009
- Bill Chung
- 12/5/2009
- Thomas Lee