File Move URGENT!

  • Thread starter Thread starter Chris Calhoun
  • Start date Start date
C

Chris Calhoun

If I copy or move a file and the file already exisits how do I overwrite the
old file or rename the new one ?

Thanks in Advance.
 
Check if the file exists first, if it does, delete it and then just
copy/move.
if(File.Exists("DestinationFile"){
File.Delete("DestinationFile");
File.Copy(Source,Destination;
}
HTH,

Bill
 
IF you are using the System.IO.File.Copy() function, you can pass in a 3rd
parameter (besides the source and destination strings) which is a boolean
value. If this is set to true it will overwrite the destination file. As
for renaming, you would probably have to check for the existance of the file
with code and then go from there.

ex.
using System.IO;
void main()
{
string strSource = "y:\\apath\\afile";
string strTarget = "x:\\apath\\afile";
if ( File.Exists( strTarget ) )
{
strTarget = strTarget + ".new";
}
File.Copy( strSource, strTarget, true );
}


Hope this helps
Carlson Q
 
Back
Top