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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top