FileSystem.CopyFile Method

Definition

Copies a file to a new location.

Overloads

CopyFile(String, String)

Copies a file to a new location.

CopyFile(String, String, UIOption)

Copies a file to a new location.

CopyFile(String, String, Boolean)

Copies a file to a new location.

CopyFile(String, String, UIOption, UICancelOption)

Copies a file to a new location.

CopyFile(String, String)

Source:
FileSystem.vb
Source:
FileSystem.vb
Source:
FileSystem.vb

Copies a file to a new location.

C#
public static void CopyFile(string sourceFileName, string destinationFileName);

Parameters

sourceFileName
String

The file to be copied.

destinationFileName
String

The location to which the file should be copied.

Exceptions

destinationFileName contains path information.

destinationFileName or sourceFileName is Nothing or an empty string.

The source file is not valid or does not exist.

A file in the target directory with the same name is in use.

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

The path exceeds the system-defined maximum length.

The user does not have required permission.

The user lacks necessary permissions to view the path.

Examples

This example copies the file Test.txt to the directory TestFiles2 without overwriting existing files.

VB
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.

VB
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.

Remarks

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.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

CopyFile(String, String, UIOption)

Source:
FileSystem.vb
Source:
FileSystem.vb
Source:
FileSystem.vb

Copies a file to a new location.

C#
public static void CopyFile(string sourceFileName, string destinationFileName, Microsoft.VisualBasic.FileIO.UIOption showUI);

Parameters

sourceFileName
String

The file to be copied.

destinationFileName
String

The location to which the file should be copied.

showUI
UIOption

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

Exceptions

destinationFileName contains path information.

destinationFileName or sourceFileName is Nothing or an empty string.

The source file is not valid or does not exist.

The destination file exists and overwrite is set to False.

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

The path exceeds the system-defined maximum length.

The user does not have required permission.

The user lacks necessary permissions to view the path.

Examples

This example copies the file Test.txt to the directory TestFiles2 without overwriting existing files.

VB
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.

VB
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.

Remarks

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.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

CopyFile(String, String, Boolean)

Source:
FileSystem.vb
Source:
FileSystem.vb
Source:
FileSystem.vb

Copies a file to a new location.

C#
public static void CopyFile(string sourceFileName, string destinationFileName, bool overwrite);

Parameters

sourceFileName
String

The file to be copied.

destinationFileName
String

The location to which the file should be copied.

overwrite
Boolean

True if existing files should be overwritten; otherwise False. Default is False.

Exceptions

destinationFileName contains path information.

destinationFileName or sourceFileName is Nothing or an empty string.

The source file is not valid or does not exist.

The destination file exists and overwrite is set to False.

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

The path exceeds the system-defined maximum length.

The user does not have required permission.

The user lacks necessary permissions to view the path.

Examples

This example copies the file Test.txt to the directory TestFiles2 without overwriting existing files.

VB
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.

VB
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.

Remarks

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.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

CopyFile(String, String, UIOption, UICancelOption)

Source:
FileSystem.vb
Source:
FileSystem.vb
Source:
FileSystem.vb

Copies a file to a new location.

C#
public static void CopyFile(string sourceFileName, string destinationFileName, Microsoft.VisualBasic.FileIO.UIOption showUI, Microsoft.VisualBasic.FileIO.UICancelOption onUserCancel);

Parameters

sourceFileName
String

The file to be copied.

destinationFileName
String

The location to which the file should be copied.

showUI
UIOption

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

onUserCancel
UICancelOption

Specifies what should be done if the user clicks Cancel during the operation. Default is ThrowException.

Exceptions

destinationFileName contains path information.

destinationFileName or sourceFileName is Nothing or an empty string.

The source file is not valid or does not exist.

The destination file exists and overwrite is set to False.

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

The path exceeds the system-defined maximum length.

The user does not have required permission.

The user lacks necessary permissions to view the path.

UICancelOption is set to ThrowException, and the user has canceled the operation or an unspecified I/O error occurs.

Examples

This example copies the file Test.txt to the directory TestFiles2 without overwriting existing files.

VB
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.

VB
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.

Remarks

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.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1