CopyTo Method (String)
Collapse the table of content
Expand the table of content

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.

Exception Condition
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 null.

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.

System_CAPS_cautionCaution

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.

FileIOPermission

for reading and writing files. Associated enumerations: FileIOPermissionAccess.Read, FileIOPermissionAccess.Write

Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top
Show:
© 2016 Microsoft