title: C# - Check if can access / read folder
body: <h2>Before closing my question, please see what I checked and <strong>doesn't work</strong> at the end of this question</h2>
  
  <hr>
  
  <h2>Question</h2>
  
  <p>I've been trying all sorts of SO answers and all of them brings me a an exception, besides many of them sounding overcomplicated for something that should be very simple.</p>
  
  <p>How can I check if I have permission to enumerate a folder's content before doing it? (I don't want I try/catch because I'm traversing the file system and multiple try/catch affects performance on C#)</p>
  
  <p>Whenever I try the following:</p>
  
  <pre><code>//where dir is a DirectoryInfo instance
  foreach (FileInfo file in dir.EnumerateFiles())
  </code></pre>
  
  <p>I get this exception:</p>
  
  <blockquote>
    <p>An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll<br>
    Additional information: O acesso ao caminho 'e:\$RECYCLE.BIN\S-1-5-18' foi negado.<br>
    <code>translation:</code>"Access to the path 'e:\$RECYCLE.BIN\S-1-5-18' was denied"</p>
  </blockquote>
  
  <hr>
  
  <h2>What doesn't work:</h2>
  
  <h3>1:</h3>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/130617/how-do-you-check-for-permissions-to-write-to-a-directory-or-file">How do you check for permissions to write to a directory or file?</a>    </li>
  <li><a href="https://stackoverflow.com/questions/4396913/how-to-check-read-and-write-permissions-on-folder-in-c-sharp/4397002#4397002">How to check Read and write permissions on folder in C#</a></li>
  </ul>
  
  <p>When I do this, the code enters the <code>if</code> and throws the same exception when it hits the enumeration.</p>
  
  <pre><code>PermissionSet permissions = new PermissionSet(System.Security.Permissions.PermissionState.None);
  permissions.AddPermission(new FileIOPermission(FileIOPermissionAccess.Read, dir.FullName));
  
  if (permissions.IsSubsetOf(AppDomain.CurrentDomain.PermissionSet))
  </code></pre>
  
  <h3>2:</h3>
  
  <ul>
  <li><a href="https://stackoverflow.com/a/5394719/2097240">https://stackoverflow.com/a/5394719/2097240</a></li>
  </ul>
  
  <p>The following raises an exception:</p>
  
  <pre><code>DirectorySecurity security = dir.GetAccessControl(AccessControlSections.All); 
  </code></pre>
  
  <p>Exception:</p>
  
  <blockquote>
    <p>An unhandled exception of type 'System.Security.AccessControl.PrivilegeNotHeldException' occurred in mscorlib.dll<br>
    Additional information: O processo não possui o privilégio 'SeSecurityPrivilege' necessário para esta operação.<br>
    <code>translation:</code> The process doesn't have the 'SeSecurityPrivilege' privilege necessary for this operation.    </p>
  </blockquote>
  
  <p>And with: </p>
  
  <pre><code>    DirectorySecurity security = dir.GetAccessControl(AccessControlSections.None); 
  </code></pre>
  
  <p>Exception:</p>
  
  <blockquote>
    <p>An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll<br>
    Additional information: Tentativa de execução de uma operação não autorizada.<br>
    <code>translation</code>: Attempting to execute an unauthorized operation</p>
  </blockquote>
  
  <h3>3:</h3>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/1410127/c-sharp-test-if-user-has-write-access-to-a-folder">C# Test if user has write access to a folder</a></li>
  </ul>
  
  <p>Tried:</p>
  
  <pre><code>DirectorySecurity security = Directory.GetAccessControl(dir.FullName);
  </code></pre>
  
  <p>Same exception as above.</p>