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
)

Parameters

sourceFileName
Type: System.String
The file to be copied.
destinationFileName
Type: System.String
The location to which the file should be copied.

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

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.

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.

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2014 Microsoft