Reputation: 4695
Why am I receiving an IOException when I come to delete this file? Surely I have unlocked it? Apparently another process is still using the file, despite not touching it otherwise.
using (var lockFile = new FileStream(lockFilePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
{
try
{
var productCount = downloadStuff();
}
catch (Exception e)
{
}
finally
{
lockFile.Unlock(0,lockFile.Length);
File.Delete(lockFilePath);
}
}
Upvotes: 0
Views: 335
Reputation: 14059
I believe your problem caused by the fact a file is still open when you call File.Delete
.
Try to move the File.Delete
call out of the using
block:
using (var lockFile = new FileStream(lockFilePath, ...))
{
...
}
File.Delete(lockFilePath);
Or simply call lockFile.Close
before File.Delete
:
finally
{
lockFile.Close();
File.Delete(lockFilePath);
}
Upvotes: 3