I am trying to delete a File on windows using C# File.Delete API. https://msdn.microsoft.com/en-us/library/system.io.file.delete(v=vs.110).aspx I am facing UnauthorizedAccessException with this API intermittently. Actually multiple processes/threads use this file as lock with File.Open on this file. If a file is in use I expect IOException to be thrown from underlying windows code, but at sometimes I see UnauthorizedAccessException. The process which is doing this is having an appropriate permission and it works in most of the cases but randomly I hit this issue.
More Information: The extension of the file I am using as a lock is .lck and it is always empty. It is just used for locking purpose with File.Open API. Documentation says that UnauthorizedAccessException is also thrown if the file is an executable file that is in use.
Questions: Is there a possibility that .lck is considered executable and UnauthorizedAccessException is thrown?
What else is the cause for UnauthorizedAccessException because I expect IOException if the file is being used?