File.Move / File.Copy exception

E

e-jimmi

I'm having a lot of trouble try to move files from one directory to
another. See below test harness:

string[] Files = Directory.GetFiles(@"C:\Temp\Test1\",
"test*.txt");
string _BackUpPath = @"c:\Temp\Test2\";
try
{
foreach (string file in Files)
{
//FileInfo ofile = new FileInfo(file);
//ofile.CopyTo(_BackUpPath);
//File.Copy(file, _BackUpPath, true);
//File.Delete(Path.Combine(_BackUpPath,
Path.GetFileName(file)));
File.Move(file, _BackUpPath); // this throws an exception
//File.Delete(file);
}
}
catch (Exception E)
{
Error(E); // recursively climbs the exception stack to uncover
any nested exceptions
}

I'm getting a "Cannot create a file when that file already exists."
exception.

If I comment the File.Move out and uncomment the File.Copy I get a "The
target directory already exists." exceptions

I am able to Delete both files when I uncomment the Deletes so don't
believe there is a Permissions or Lock issue.

I've attempted to perform a file Delete on the destination directory
before attempting to do the copy / move with no luck.

Not sure where to go from here.

I'd be grateful for any assistance
PS. First time post so set me straight if I'm out of order.

thanks

JS
 
J

Jacky Kwok

e-jimmi said:
I'm having a lot of trouble try to move files from one directory to
another. See below test harness:

string[] Files = Directory.GetFiles(@"C:\Temp\Test1\",
"test*.txt");
string _BackUpPath = @"c:\Temp\Test2\";
try
{
foreach (string file in Files)
{
//FileInfo ofile = new FileInfo(file);
//ofile.CopyTo(_BackUpPath);
//File.Copy(file, _BackUpPath, true);
//File.Delete(Path.Combine(_BackUpPath,
Path.GetFileName(file)));
File.Move(file, _BackUpPath); // this throws an exception
//File.Delete(file);
}
}
catch (Exception E)
{
Error(E); // recursively climbs the exception stack to uncover
any nested exceptions
}

I'm getting a "Cannot create a file when that file already exists."
exception.

If I comment the File.Move out and uncomment the File.Copy I get a "The
target directory already exists." exceptions

I am able to Delete both files when I uncomment the Deletes so don't
believe there is a Permissions or Lock issue.

I've attempted to perform a file Delete on the destination directory
before attempting to do the copy / move with no luck.

Not sure where to go from here.

I'd be grateful for any assistance
PS. First time post so set me straight if I'm out of order.

thanks

JS

_BackUpPath = @"c:\Temp\Test2\";
File.Copy(file, _BackUpPath, true);
File.Move(file, _BackUpPath);


The "BackUpPath" in both "Copy" and "Move" in your code are directory only.
However, the "File.Copy" and "File.Move" require it is filename.

try

File.Copy(file, _BackUpPath+Path.GetFileName(file), true);
File.Move(file, _BackUpPath+Path.GetFileName(file));
 
J

Jaimie Sims

Thanks Jacky

That did the trick - should have been able to work that out myself. It's
been a tough day but have finally had a few wins.

Jaimie.
 

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

Top