This exception is occurring intermittently for the same user for the same machine, when reading files within %LOCALAPPDATA%
.
Research
I have checked all the possible duplicates currently offered by this title (there are a lot). There is one relating to reading an AES encrypted file which has no answer; and I don't believe applies, since these files are not encrypted.
Most of them are to do with writing files (but I'm reading a file), or are the obvious causes as documented on MSDN for File.ReadAllBytes(string).
The three explanations for this exception on there are:
- "This operation is not supported on the current platform" - I don't know what that means; but given that this works sometimes for the same user on the same machine (I'll explain below), I think I can rule this out.
- "path specified a directory" - as you'll see from the code below, the call is made within a check of
File.Exists
, so I think I can rule this out. - "The caller does not have the required permission." This is the usual explanation for this exception, and I suspect I'm getting some kind of "fringe case" of this.
Scenario
This is occurring when an application which is running as a domain user is reading a file inside a subfolder of %LOCALAPPDATA%
of the same user (for which there should be no permission issues for that user to read files). The subfolders within that just follow the normal "CompanyName"\"ApplicationName" structure, and there are no additional permissions applied on the sub folders (we're just using the folder to keep our files away from other people's).
Exception
System.UnauthorizedAccessException: Access to the path '[redacted]' is denied. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) at System.IO.File.InternalReadAllBytes(String path, Boolean checkHost)
at the code below
Code
// Note that filename is within %LOCALAPPDATA%
if (File.Exists(fileName))
{
var readAllBytes = File.ReadAllBytes(fileName); // exception here
// etc...
}
Proof that it is intermittent
And I can prove from the combination of our error logs, and other information that this is working most of the time, and I can prove the following sequence of events for a particular combination of machine and user:
- The application works, then
- This exception occurs (maybe several times, with retry delays exponentially increasing each time: 1minute, 2minutes, 4minutes, etc), then
- The application works again
I don't believe that any material change (e.g. permissions) will have occurred to the file system in order to fix it. I'm wondering whether this might be caused by a fringe permissions issue, for example, if their password is about to expire, or has recently been changed.
I have a specific example when I noticed this error happening, and I advised the user to reboot their machine, and the problem went away.
Question
Can anyone give me an authoritative explanation of the cause of this, above what I've already guessed, or to confirm what it is?