This documentation is archived and is not being maintained.

File.Copy Method (String, String, Boolean)

Copies an existing file to a new file. Overwriting a file of the same name is allowed.

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

public static void Copy (
	string sourceFileName,
	string destFileName,
	bool overwrite
)
public static void Copy (
	String sourceFileName, 
	String destFileName, 
	boolean overwrite
)
public static function Copy (
	sourceFileName : String, 
	destFileName : String, 
	overwrite : boolean
)

Parameters

sourceFileName

The file to copy.

destFileName

The name of the destination file. This cannot be a directory.

overwrite

true if the destination file can be overwritten; otherwise, false.

Exception typeCondition

UnauthorizedAccessException

The caller does not have the required permission.

ArgumentException

sourceFileName or destFileName is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.

-or-

sourceFileName or destFileName specifies a directory.

ArgumentNullException

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

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.

DirectoryNotFoundException

The path specified in sourceFileName or destFileName is invalid (for example, it is on an unmapped drive).

FileNotFoundException

sourceFileName was not found.

IOException

destFileName is read-only, or destFileName exists and overwrite is false.

-or-

An I/O error has occurred.

NotSupportedException

sourceFileName or destFileName is in an invalid format.

The sourceFileName and destFileName arguments are permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory.

For an example of using this method, see the Example section. The following table lists examples of other typical or related I/O tasks.

To do this...

See the example in this topic...

Create a text file.

How to: Write Text to a File

Write to a text file.

How to: Write Text to a File

Read from a text file.

How to: Read Text from a File

Append text to a file.

How to: Open and Append to a Log File

File.AppendText

FileInfo.AppendText

Rename or move a file.

File.Move

FileInfo.MoveTo

Delete a file.

File.Delete

FileInfo.Delete

Read from a binary file.

How to: Read and Write to a Newly Created Data File

Write to a binary file.

How to: Read and Write to a Newly Created Data File

Move a directory.

Directory.Move

DirectoryInfo.MoveTo

Create a subdirectory.

CreateSubdirectory

Copy a directory.

Directory

The following example copies a file to the specified path, allowing the overwriting of a target file of the same name.

using System;
using System.IO;

class Test 
{
    public static void Main() 
    {
        string path = @"c:\temp\MyTest.txt";
        string path2 = path + "temp";

        try 
        {
            // Create the file and clean up handles.
            using (FileStream fs = File.Create(path)) {}

            // Ensure that the target does not exist.
            File.Delete(path2);

            // Copy the file.
            File.Copy(path, path2);
            Console.WriteLine("{0} copied to {1}", path, path2);

            // Try to copy the same file again, which should succeed.
            File.Copy(path, path2, true);
            Console.WriteLine("The second Copy operation succeeded, which was expected.");
        } 

        catch 
        {
            Console.WriteLine("Double copy is not allowed, which was not expected.");
        }
    }
}

import System.*;
import System.IO.*;

class Test
{
    public static void main(String[] args)
    {
        String path = "c:\\temp\\MyTest.txt";
        String path2 = path + "temp";

        try {
            // Create the file and clean up handles.
            FileStream fs = File.Create(path);
            try {
            }
            finally {
                fs.Dispose();
            }

            // Ensure that the target does not exist.
            File.Delete(path2);

            // Copy the file.
            File.Copy(path, path2);
            Console.WriteLine("{0} copied to {1}", path, path2);

            // Try to copy the same file again, which should succeed.
            File.Copy(path, path2, true);
            Console.WriteLine("The second Copy operation succeeded, which " 
                + "was expected.");
        }
        catch (System.Exception exp) {
            Console.WriteLine("Double copy is not allowed, which was not " 
                + "expected.");
        }
    } //main
} //Test

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Show: