Copy File

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Instead of using process.start to open a cmd to copy a file from one dir to other dir, any other function can do this in the c#.
thank you
 
System.IO.File.Copy()

Instead of using process.start to open a cmd to copy a file from one dir to
other dir, any other function can do this in the c#.
thank you
 
Hi HK Guy,

Sure is, as Shiva mentioned

File.Copy(@"C:\FileA.dat", @"C:\FileB.dat", true);

This will copy a file called FileA and create a new FileB or overwrite FileB if it already exists (specified with true as the last parameter). The @ tells the compiler to ignore escape characters or we would have to use File.Copy("C:\\FileA.dat", "FileB.dat", true).

If you don't want to overwrite existing files use

File.Copy(@"C:\FileA.dat", @"C:\FileB.dat");
File.Copy(@"C:\FileA.dat", @"C:\FileB.dat", false);

Or ... you could just ask the user

if(!File.Exists(@"C:\FileB.dat")
|| MessageBox.Show("FileB.dat already exists. Do you want to overwrite?",
"MyProgram", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
File.Copy(@"C:\FileA.dat", @"C:\FileB.dat", true);
}

The last code will evaluate to "if fileb.dat does NOT exist" go ahead and copy, otherwise "if user clicks YES" on the message I send him, go ahead and copy

|| instead of | will ensure that the message box isn't shown when it isn't needed.

Hm, I seem to be too elaborate and should stop before I start to explaining about the flower and the bees :)
 
Back
Top