title: How to detect if a file exists w/o getting derailed by permissions issues?
body: <p>There are at least three techniques for detecting if a file exists:</p>
  
  <ol>
  <li>Query the file attributes </li>
  <li>Use FindFile() with a specific filename instead of a search pattern</li>
  <li>Open the file in read mode and look at any resulting error</li>
  </ol>
  
  <p>All of the above seem to suffer from false negatives.  That is, I am told that the file doesn't exist when it actually does due to either glitches in how file-io over a network works, or due to file permissions issues.</p>
  
  <p>I have a customer who can see that a file exists in explorer, delete that file, but is given "access denied" if they try to view that file.</p>
  
  <p>I have been unsuccessful at replicating this exact behavior.  But what I can create is a situation where the file exists, but due to restricting permissions on it, I cannot see the file in that folder under my user credentials.  That is, GetFileAttributes(), FindFile(), and fopen() return failure, i.e. file not found for that file (but if I look in that same folder under a different account - say a network admin, I can see that the file most certainly exists).</p>
  
  <p>As to how my end user (or anyone) would end up in such situations is opaque to me.  I have no concrete ideas - maybe power failure while the file was previously opened, maybe some sort of network glitch causing the file handle to remain locked to a dead process on a foreign PC, ...?  I'm just making stuff up because I have no idea what might cause such situations to arise.</p>
  
  <p>However, what I really don't have is the ability to query Windows and know for a fact "does file X exist, or not"</p>
  
  <p>Does anyone know of a technique that will honestly answer that question regardless of the user's permissions (assuming that they're allowed to query the contents of the folder itself - I'm not asking for an unauthorized access scenario - just a "normal" user X can't edit file Y, but still wants to know if file Y exists or not.</p>
  
  <hr>
  
  <p>Hokay - this is getting bizarre.</p>
  
  <p>Using any of the file detection techniques works so long as I ask twice.  The first time always tells me "does not exist".  Second+ tells me "yup, it's there, but you can't open it."</p>
  
  <p>The file in question is on a shared folder on a Windows Server 2008 NTFS drive.  It is shared for everyone full control.  I have manually added an "Everyone Deny Read" ACL to the file, in order to simulate my customers problem.  So I have denied read, but no other access, and only to the file, not to the share, or the folder in which this file lives.</p>
  
  <p>(I used Explorer to make this modification, not my own software or a command line utility).</p>
  
  <p>I can see that the file exists from the local admin account on that server.  I cannot see that it even exists from my local workstation, logged in as a standard user under Windows 7, UAC enabled, non-elevated explorer / application.</p>
  
  <hr>
  
  <p>It would appear that if a file's read-access is explicitly denied, that the file is not visible any longer (except to account for which that deny doesn't apply, or to the local admin which has some back-door way to see the file despite that deny ACL).</p>
  
  <p>I have tried FindFirstFile, GetAttributes, CreateFile, _taccess_s, and PathFileExists.  In every case, the first attempt to access the file indicates "file not found", but the second attempt in a row results in no-error (file found).</p>
  
  <p>I cannot begin to explain these results.  I think at this point I need to run all of my tests locally, to remove network file sharing from the mix.  These results just don't make a whole heckuva lot of sense (to me).</p>
  
  <hr>
  
  <p>fltmc output for the folder, from local admin account on the server:</p>
  
  <pre><code>Filter Name                     Num Instances    Altitude    Frame
  ------------------------------  -------------  ------------  -----
  aksdf                                   8       145900         0
  luafv                                   1       135000         0
  </code></pre>