views:

98

answers:

1

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 two 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.

+1  A: 

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

Oscar Cabrero
I went over the classes I use and not a single one of them inherits from IDisposable. How do I implement using in this?
WebDevHobo