I have a process that writes a file in a temp directory. The file is being written without shared read access, so that if someone attempts to read this file while it is being written, an IOException
will be thrown, which is fine.
In some rare cases, I seem to be getting UnauthorizedAccessException
while attempting to read this file. I'm fairly certain (from the logs) that the file is not being written to, but I can't see any other reason I wouldn't have access to it.
Also, the process runs from a Visual Studio extension, which means it has the same permissions as the parent process (Visual Studio itself).
Here's how I attempt to open the file:
new FileStream(cacheFileName, FileMode.Open, FileAccess.Read, FileShare.Read);
Sometimes, this line throws an UnauthorizedAccessException
. Any ideas why?