The Wayback Machine - https://web.archive.org/web/20240702202436/https://www.geeksforgeeks.org/file-copystring-string-method-in-c-sharp-with-examples/
Open In App

File.Copy(String, String) Method in C# with Examples

Last Updated : 14 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

File.Copy(String, String) is an inbuilt File class method that is used to copy the content of the existing source file content to another destination file which is created by this function.
 

Syntax:  

public static void Copy (string sourceFileName, string destFileName);

Parameter: This function accepts two parameters which are illustrated below:  

  • sourceFileName: This is the file from where data is copied.
  • destFileName: This is the file where data is pasted. This cannot be a existing file or a directory.

Exceptions: 

  • UnauthorizedAccessException: The caller does not have the needed permission.
  • ArgumentException: The source filename or destFileName is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars. OR the sourceFileName or destFileName specifies a directory.
  • ArgumentNullException: The sourceFileName or destFileName is null.
  • PathTooLongException: The given path, file name, or both exceed the system-defined maximum length.
  • DirectoryNotFoundException: The path given in source filename or destFileName is invalid (for example, it is on an unmapped drive).
  • FileNotFoundException: The source filename was not found.
  • IOException: destFileName exists. OR an I/O error has occurred.
  • NotSupportedException: The source file name or destFileName is in an invalid format.

Below are the programs to illustrate the File.Copy(String, String) method.
Program 1: Before running the below code, only source file file.txt was created which is shown below. Below code itself creates a destination file gfg.txt and copies the source file contents to the destination file.
 

file.txt

CSharp




// C# program to illustrate the usage
// of File.Copy() method
  
// Using System, System.IO,
// System.Text and System.Linq namespaces
using System;
using System.IO;
using System.Text;
using System.Linq;
  
class GFG {
    // Main() method
    public static void Main()
    {
        // Creating two files
        string sourceFile = @"file.txt";
        string destinationFile = @"gfg.txt";
        try {
            // Copying source file's contents to
            // destination file
            File.Copy(sourceFile, destinationFile);
        }
        catch (IOException iox) {
            Console.WriteLine(iox.Message);
        }
        Console.WriteLine("Copying process has been done.");
    }
}


Executing:

mcs -out:main.exe main.cs
mono main.exe
Copying process has been done.

After running the above code, above output has been shown and a new destination file gfg.txt created which is shown below:
 

gfg.txt

Program 2: Before running the below code, two file is created with some contents shown below:
 

file.txt

 

gfg.txt

CSharp




// C# program to illustrate the usage
// of File.Copy() method
  
// Using System, System.IO,
// System.Text and System.Linq namespaces
using System;
using System.IO;
using System.Text;
using System.Linq;
  
class GFG {
    // Main() method
    public static void Main()
    {
        // Specifying two files
        string sourceFile = @"file.txt";
        string destinationFile = @"gfg.txt";
        try {
            // Copying source file's contents to
            // destination file
            File.Copy(sourceFile, destinationFile);
        }
        catch (IOException iox) {
            Console.WriteLine(iox.Message);
        }
    }
}


Executing: 

mcs -out:main.exe main.cs
mono main.exe
Could not create file "/home/runner/NutritiousHeavyRegression/gfg.txt". File already exists.

After running the above code, the above error is thrown because the destination file was created before running the program.



Similar Reads

File.Copy(String, String, Boolean) Method in C# with Examples
File.Copy(String, String, Boolean) is an inbuilt File class method that is used to copy the content of the existing source file content to another destination file if exist, else create a new destination file then copying process is done.Syntax: public static void Copy (string sourceFileName, string destFileName, bool overwrite); Parameter: This fu
4 min read
Shallow Copy and Deep Copy in C#
In general, when we try to copy one object to another object, both the objects will share the same memory address. Normally, we use assignment operator, = , to copy the reference, not the object except when there is value type field. This operator will always copy the reference, not the actual object. For Example: Suppose G1 refers to memory addres
4 min read
File.Replace(String, String, String) Method in C# with Examples
File.Replace(String, String, String) is an inbuilt File class method that is used to replace the contents of a specified destination file with the contents of a source file then it deletes the source file and creates a backup of the replaced file.Syntax: public static void Replace (string sourceFileName, string destinationFileName, string destinati
3 min read
File.Replace(String, String, String, Boolean) Method in C# with Examples
File.Replace(String, String, String, Boolean) is an inbuilt File class method that is used to replace the contents of a specified destination file with the contents of a source file then it deletes the source file, creates a backup of the replaced file, and optionally ignores merge errors. Syntax: public static void Replace (string sourceFileName,
3 min read
File.AppendAllText(String, String) Method in C# with Examples
File.AppendAllText(String, String) is an inbuilt File class method which is used to append the specified string to the given file if that file exists else creates a new file and then appending is done. It also closes the file.Syntax: public static void AppendAllText (string path, string contents); Parameter: This function accepts two parameters whi
3 min read
File.AppendAllText(String, String, Encoding) Method in C# with Examples
File.AppendAllText(String, String, Encoding) is an inbuilt File class method which is used to append the specified string to the given file using the specified encoding if that file exists else creates a new file and then appending is done. Syntax: public static void AppendAllText (string path, string contents, System.Text.Encoding encoding); Param
3 min read
File.WriteAllText(String, String) Method in C# with Examples
File.WriteAllText(String, String) is an inbuilt File class method that is used to create a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten. Syntax: public static void WriteAllText (string path, string contents); Parameter: This function accepts two parameters which ar
3 min read
File.WriteAllText(String, String, Encoding) Method in C# with Examples
File.WriteAllText(String, String, Encoding) is an inbuilt File class method that is used to create a new file, writes the specified string to the file using the specified encoding, and then closes the file. If the target file already exists, it is overwritten. Syntax: public static void WriteAllText (string path, string contents, System.Text.Encodi
3 min read
File.WriteAllLines(String, String[], Encoding) Method in C# with Examples
File.WriteAllLines(String, String[], Encoding) is an inbuilt File class method that is used to create a new file, writes the specified string array to the file by using the specified encoding, and then closes the file.Syntax: public static void WriteAllLines (string path, string[] contents, System.Text.Encoding encoding); Parameter: This function a
3 min read
File.WriteAllLines(String, String[]) Method in C# with Examples
File.WriteAllLines(String, String[]) is an inbuilt File class method that is used to create a new file, writes the specified string array to the file, and then closes the file.Syntax: public static void WriteAllLines (string path, string[] contents); Parameter: This function accepts two parameters which are illustrated below: path: This is the spec
3 min read
Article Tags :
three90RightbarBannerImg