title: UnauthorizedAccessException "Access to the path is denied" from File.ReadAllBytes in LOCALAPPDATA
body: <p>This exception is occurring intermittently for the same user for the same machine, when reading files within <code>%LOCALAPPDATA%</code>.</p>
  
  <p><strong>Research</strong></p>
  
  <p>I have checked all the possible duplicates currently offered by this title (there are a lot). There is one relating to <a href="https://stackoverflow.com/questions/22943631/access-to-the-path-is-denied-on-file-readallbytes-on-aes-encrypted-file">reading an AES encrypted file</a> which has no answer; and I don't believe applies, since these files are not encrypted.</p>
  
  <p>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 <a href="https://msdn.microsoft.com/en-us/library/system.io.file.readallbytes(v=vs.110).aspx" rel="nofollow noreferrer">File.ReadAllBytes(string)</a>.</p>
  
  <p>The three explanations for this exception on there are:</p>
  
  <ol>
  <li>"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.</li>
  <li>"<em>path</em> specified a directory" - as you'll see from the code below, the call is made within a check of <code>File.Exists</code>, so I think I can rule this out.</li>
  <li>"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.</li>
  </ol>
  
  <p><strong>Scenario</strong></p>
  
  <p>This is occurring when an application which is running as a domain user is reading a file inside a subfolder of <code>%LOCALAPPDATA%</code> 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).</p>
  
  <p><strong>Exception</strong></p>
  
  <blockquote>
    <p>System.UnauthorizedAccessException: Access to the path '<em>[redacted]</em>' 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)<br>
    at <em>the code below</em></p>
  </blockquote>
  
  <p><strong>Code</strong></p>
  
  <pre><code>        // Note that filename is within %LOCALAPPDATA%
          if (File.Exists(fileName))
          {
              var readAllBytes = File.ReadAllBytes(fileName); // exception here
              // etc...
          }
  </code></pre>
  
  <p><strong>Proof that it is intermittent</strong></p>
  
  <p>And I can prove from the combination of our error logs, and other information that this is working <strong>most</strong> of the time, and I can prove the following sequence of events for a particular combination of machine and user:</p>
  
  <ul>
  <li>The application works, then</li>
  <li>This exception occurs (maybe several times, with retry delays exponentially increasing each time: 1minute, 2minutes, 4minutes, etc), then</li>
  <li>The application works again</li>
  </ul>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p><strong>Question</strong></p>
  
  <p>Can anyone give me an authoritative explanation of the cause of this, above what I've already guessed, or to confirm what it is?</p>