Export (0) Print
Expand All

FileSystem.CopyFile Method (String, String)

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


Type: System.String

The file to be copied.

Type: System.String

The location to which the file should be copied.


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 \\.\).


The system could not retrieve the absolute path.


destinationFileName contains path information.


destinationFileName or sourceFileName is Nothing or an empty string.


The source file is not valid or does not exist.


The combined path points to an existing directory.


The user does not have sufficient permissions to access the file.


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.

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.

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, _

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

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.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.

© 2014 Microsoft