Copy a file to another location in C#
This post will discuss how to copy a file to another location in C#.
1. Using File.Copy() method
You can use File.Copy(src, dest) method to copy an existing file to a new file, which can have a different file name than the source file. The File class is available from the System.IO namespace, and both src and dest can be relative or absolute paths.
The following example copies the file specified by src to the file specified by dest using the File.Copy() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System; using System.IO; public class Program { public static void Main() { string src = @"C:\image.png"; string dest = @"C:\image.png"; File.Copy(src, dest); } } |
You should enclose your code within a try-catch block, and ensure that the path specified by src and dest is valid before invoking the File.Copy() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
using System; using System.IO; public class Program { public static void Main() { string src = @"C:\image.png"; string dest = @"C:\image.png"; try { if (File.Exists(src) && !File.Exists(dest)) { File.Copy(src, dest); } else { Console.WriteLine("Source file doesn't exist or destination file exist."); } } catch (Exception ex) { // handle other exceptions } } } |
The above version of File.Copy() does not allow overwriting files that already exist, and throws System.IO.IOException. The File.Copy() method can accept an optional boolean parameter which, if true, will overwrite the target file.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
using System; using System.IO; public class Program { public static void Main() { string src = @"C:\image.png"; string dest = @"C:\image.png"; try { if (File.Exists(src)) { File.Copy(src, dest, true); } else { Console.WriteLine("The source file doesn't exist."); } } catch (Exception ex) { // handle other exceptions } } } |
2. Using FileInfo.CopyTo() method
Alternatively, you can use the FileInfo.CopyTo() method to copy an existing file to a new location. The default behavior of this method does not allow overwriting of an existing file.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
using System; using System.IO; public class Program { public static void Main() { string src = @"C:\image.png"; string dest = @"C:\image.png"; try { if (File.Exists(src) && !File.Exists(dest)) { FileInfo fi = new FileInfo(src); fi.CopyTo(dest); } else { Console.WriteLine("The source file doesn't exist or the destination file exist."); } } catch (Exception ex) { // handle other exceptions } } } |
To override an existing file, you can invoke the CopyTo(String, Boolean) overload, which accepts a boolean overwrite parameter. If the overwrite parameter is true, the existing file will be overwritten.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
using System; using System.IO; public class Program { public static void Main() { string src = @"C:\image.png"; string dest = @"C:\image.png"; try { if (File.Exists(src)) { FileInfo fi = new FileInfo(src); fi.CopyTo(dest, true); } else { Console.WriteLine("The source file doesn't exist."); } } catch (Exception ex) { // handle other exceptions } } } |
That’s all about copying a file to another location in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)