Reputation: 1529
Today I ran into a strange problem: Since a year and several versions / tests of the application the following code has been used successfully to replace a file with an other.
File.Replace(path + ".tmp", path, null);
This has worked locally and also with UNC paths (network shares). But today I got the following error when I used this code to replace a file on a UNC path (local still works):
The process cannot access the file because it is being used by another process
When I use the following code instead of the above, it works:
File.Delete(path);
File.Move(path + ".tmp", path);
So my questions:
I'm using .Net Framework 4.0 with Visual Studio 2010.
Thanks in advance.
Upvotes: 23
Views: 28860
Reputation: 18439
According to MSDN on File.Replace
File.Replace will throw an exception when...
Which File.Delete, File.Move won't.
Upvotes: 8
Reputation: 6554
Here's the MSDN article on File.Replace()
Creating a backup of the original appears to be the difference.
Upvotes: 4