开发者

File.Replace throwing IOException

开发者 https://www.devze.com 2022-12-25 23:28 出处:网络
I have an app that can make modify images. In some cases, this makes the filesize smaller, in some cases bigger.

I have an app that can make modify images. In some cases, this makes the filesize smaller, in some cases bigger.

The program doesn't have an option to "not replace the file if result has a bigger filesize".

So I wrote a little C# app to try and solve this.

Instead of overwriting the files, I make the app write the result to a folder under the current one and name that folder Test.

The C# app I wrote compares grabs the contents of both folders and puts the full path to the file(s) in tw开发者_高级运维o List objects. I then compare and replace.

The replacing isn't working however. I get the following IOException:

Unable to remove the file to be replaced

The location is on an external hard-drive, on which I have full rights.

Now, I know I can just do File.Delete and File.Move in that order, but this exception has gotten me interested in why this particular setup wont work.

Here's the source code: http://pastebin.com/4Vq82Umu

And yes, the file specified as last argument of the Replace function does exist.


verify that your file to be replaced is not loaded into memory, that could be causing a lock in the file

try to use the using statement to automatically dispose the resource,

according to documentation:

If destinationBackupFileName is on a different drive it is not removed as the documentation states, instead an IOException is thrown with the message "Unable to remove the file to be replaced." is thrown

http://msdn.microsoft.com/en-us/library/9d9h163f.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号