Website Design United States, Website Design California, Website Designing United States, Website Designing California

File Copy Using File.Copy Method in C# - C#

Mark Schweikert

Copying a file using the File.Copy Method from the .Net framework.

using System;
using System.IO;

class FileCopy
public static void Main()
string sourceFile = @"C:\SourceFile.txt";
string destinationFile = @"C:\DestinationFile.txt";

//Attempt file copy
//Delete destinationFile for overwrite,
//causes exception if already exists.

File.Copy(sourceFile, destinationFile);

//Successful if the method gets this far without causing an exception
Console.WriteLine("Copy completed.");

//If the file copy fails then catch will gain control of the method
catch (Exception exc)

Console.WriteLine("\nPress Enter to Continue:");


© 2008-2009