FileInfo.CopyTo Method (String)

Copies an existing file to a new file, disallowing the overwriting of an existing file.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)

member CopyTo : 
        destFileName:string -> FileInfo

Parameters

destFileName
Type: System.String

The name of the new file to copy to.

Return Value

Type: System.IO.FileInfo
A new file with a fully qualified path.

ExceptionCondition
ArgumentException

destFileName is empty, contains only white spaces, or contains invalid characters.

IOException

An error occurs, or the destination file already exists.

SecurityException

The caller does not have the required permission.

ArgumentNullException

destFileName is a null reference (Nothing in Visual Basic).

UnauthorizedAccessException

A directory path is passed in, or the file is being moved to a different drive.

DirectoryNotFoundException

The directory specified in destFileName does not exist.

PathTooLongException

The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.

NotSupportedException

destFileName contains a colon (:) within the string but does not specify the volume.

Use the CopyTo method to allow overwriting of an existing file.

Caution noteCaution

Whenever possible, avoid using short file names (such as XXXXXX~1.XXX) with this method. If two files have equivalent short file names then this method may fail and raise an exception and/or result in undesirable behavior

The following example demonstrates both overloads of the CopyTo method.

No code example is currently available or this language may not be supported.

The following example demonstrates copying one file to another file, throwing an exception if the destination file already exists.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft