Jump to content
  • Advertisement
Sign in to follow this  
KazenoZ

File.Copy a file to multiple targets

This topic is 2566 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

I'm having this issue with copying files with .NET's File.Copy() method.

I have a file, that I want to copy to a couple locations, but when doing something like:

File.Copy(source, target1);
File.Copy(source, target2);


I keep getting an exception error about the file being in use.

Seeing that if I copy it to just 1 location, wait a few moments and then copy it again, it works fine, I'm assuming that the error presented to me isn't because of the Copy() method not closing the file correctly, but rather because I'm trying to use the second copy while the first one is still being processed.

I also tried doing this:

File::Copy(source, target1);
bool copyDone = false;
while(!copyDone){
try{
File::Copy(source, target2);
}
finally{
copyDone = true;
}
}

But it would just go straight to the finally block and then still error.

My question, then, would be, how should I go about this?
I couldn't find a method for checking if a file is currently accessed.


Just curious as to what others have done in a similiar situation.

Thanks for the answers to come!

Share this post


Link to post
Share on other sites
Advertisement
As a work around, I'd manually load the source file contents into memory and write them to the destinations.

Share this post


Link to post
Share on other sites
Well, a guy on DaniWeb already helped me solve it,

bool copyFile(String^ source, String^ target, bool overwrite){

try{

File::Copy(source, target, overwrite);

}

catch(IOException^ e){

//Code to display the error from e goes here

return false;

}

return true;

}


And then calling it in a while loop until a true is returned.

I don't really get why this works now, it's practically the same as what I did, except this is in a function.
But I guess, if it works, why complain.

Thanks anyway.

Share this post


Link to post
Share on other sites
But I guess, if it works, why complain.[/quote]

AMD Catalyst Vision Engine Control Center (whatever the name, used to be ATI CCC) that comes with Radeon drivers consumes 10% of quad core CPU. After doing some debugging, it seems to be creating threads in an infinite loop. No clue why. The application is a UI for setting a handful of registry entries and my nomination for worst application in existence, third year running.

I suppose it was coded in similar way.

It would be interesting to try this on a networked file.

Share this post


Link to post
Share on other sites
Then the exception handler catches it and deals with it.

If it's unaccessable because of it really being used by a different program, there's nothing I can do except for throwing an error, unless I want to piss the user off even more by closing his other program acessing the file.

Besides, I was referring to it being in a function vs. it being hardcoded a couple times, not about the loop, I'm running the tests for everything else now that that actually works.
I want to see using that(Using code outside of a function) making a program blow, functionally-wise, not coding-wise.

Share this post


Link to post
Share on other sites
The reason your loop didn't work is that finally blocks get run regardless of it throwing an exception or not. All you'd need is:

while(!copyDone){
try{
File::Copy(source, target2);
copyDone = true; // This line gets bypassed if an exception is thrown above
}
}


However I'd suggest using the alternative workaround from http://stackoverflow.com/questions/603444/file-copy-locks-source-file-after-completion as it doesn't leave you in a potentially infinite loop.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!