How to copy a file in c#

To create a copy of a file, we must create a new FileInfo object by specifying the path to the file and use the FileInfo object’s CopyTo method to create a copy of the file.

Example code:

FileInfo fi = new FileInfo(@"C:\file.txt");
 
if(fi.Exists)
{
		fi.CopyTo(@"C:\copyfile.txt");
}

Similar method can be used to move and create files.

Share Article/Example:
  • Facebook
  • Twitter
  • del.icio.us
  • Digg
  • DotNetKicks
  • DZone