Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework
System.IO Namespace
File Class
File Methods
Copy Method
 Copy Method (String, String)
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
File..::.Copy Method (String, String)

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

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

Visual Basic (Declaration)
Public Shared Sub Copy ( _
    sourceFileName As String, _
    destFileName As String _
)
Visual Basic (Usage)
Dim sourceFileName As String
Dim destFileName As String

File.Copy(sourceFileName, destFileName)
C#
public static void Copy(
    string sourceFileName,
    string destFileName
)
Visual C++
public:
static void Copy(
    String^ sourceFileName, 
    String^ destFileName
)
J#
public static void Copy(
    String sourceFileName,
    String destFileName
)
JScript
public static function Copy(
    sourceFileName : String, 
    destFileName : String
)

Parameters

sourceFileName
Type: System..::.String

The file to copy.

destFileName
Type: System..::.String

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

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

-or-

An I/O error has occurred.

NotSupportedException

sourceFileName or destFileName is in an invalid format.

This method is equivalent to the Copy(String, String, Boolean) method overload with the overwrite parameter set to false.

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. To obtain the current working directory, see GetCurrentDirectory.

For a list of common I/O tasks, see Common I/O Tasks.

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

Visual Basic
Imports System
Imports System.IO

Public Class Test
    Public Shared Sub Main()
        ' Specify the directories you want to manipulate.
        Dim path As String = "c:\temp\MyTest.txt"
        Dim path2 As String = path + "temp"

        Try
            Dim fs As FileStream = File.Create(path)
            fs.Close()
            ' 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 fail.
            File.Copy(path, path2)
            Console.WriteLine("The second Copy operation succeeded, which was not expected.")

        Catch e As Exception
            Console.WriteLine("The second Copy operation failed, as expected.")
        End Try
    End Sub
End Class

C#
using System;
using System.IO;

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

        try 
        {
            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 fail.
            File.Copy(path, path2);
            Console.WriteLine("The second Copy operation succeeded, which was not expected.");
        } 

        catch (Exception e) 
        {
            Console.WriteLine("Double copying is not allowed, as expected.");
            Console.WriteLine(e.ToString());
        }
    }
}

Visual C++
using namespace System;
using namespace System::IO;
int main()
{
   String^ path = "c:\\temp\\MyTest.txt";
   String^ path2 = String::Concat( path, "temp" );
   try
   {

      // 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 fail.
      File::Copy( path, path2 );
      Console::WriteLine( "The second Copy operation succeeded, which was not expected." );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Double copying is not allowed, as expected." );
      Console::WriteLine( e );
   }

}


J#
import System.*;
import System.IO.*;

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

        try {
            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 fail.
            File.Copy(path, path2);
            Console.WriteLine("The second Copy operation succeeded, which " 
                + "was not expected.");
        }
        catch (System.Exception e) {
            Console.WriteLine("Double copying is not allowed, as expected.");
            Console.WriteLine(e.ToString());
        }
    } //main
} //Test

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

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

.NET Framework

Supported in: 3.5, 3.0 SP1, 3.0, 2.0 SP1, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content      
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker