FileSystem.CopyFile Method (String, String, UIOption)

 

Copies a file to a new location.

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

public static void CopyFile(
	string sourceFileName,
	string destinationFileName,
	UIOption showUI
)

Parameters

sourceFileName
Type: System.String

The file to be copied.

destinationFileName
Type: System.String

The location to which the file should be copied.

showUI
Type: Microsoft.VisualBasic.FileIO.UIOption

Whether to visually track the operation's progress. Default is UIOption.OnlyErrorDialogs.

Exception Condition
ArgumentException

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.

ArgumentNullException

destinationFileName or sourceFileName is Nothing or an empty string.

FileNotFoundException

The source file is not valid or does not exist.

IOException

The combined path points to an existing directory.

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

The destination file exists and overwrite is set to False.

NotSupportedException

A file or directory name in the path contains a colon (:) or is in an invalid format.

PathTooLongException

The path exceeds the system-defined maximum length.

UnauthorizedAccessException

The user does not have required permission.

SecurityException

The user lacks necessary permissions to view the path.

CopyFile does not preserve ACEs (Access Control Entries). The newly created file inherits default ACEs from the directory in which it is created.

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.

My.Computer.FileSystem.CopyFile( _
    "C:\UserFiles\TestFiles\test.txt", _
    "C:\UserFiles\TestFiles2\test.txt", overwrite:=False)

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.

My.Computer.FileSystem.CopyFile( _
    "C:\UserFiles\TestFiles\test.txt", _
    "C:\UserFiles\TestFiles2\NewFile.txt", _
    FileIO.UIOption.OnlyErrorDialogs, _
    FileIO.UICancelOption.DoNothing)

Replace the file paths with the paths you want to use in your code.

EnvironmentPermission

Controls the ability to access all environment variables. Associated enumeration: Unrestricted.

FileIOPermission

Controls the ability to access files and folders. Associated enumeration: Unrestricted.

RegistryPermission

Controls the ability to access registry variables. Associated enumeration: Unrestricted.

UIPermission

Controls the permissions related to user interfaces and the clipboard. Associated enumeration: SafeSubWindows.

.NET Framework
Available since 2.0
Return to top
Show: