title: List All Open Files
body: <p>This is an add-on to another question found <a href="https://stackoverflow.com/questions/733384/how-to-enumerate-process-handles/733420#733420">here</a>.</p>
  
  <p>In short:  <strong>I would like to list all of the open files on a system and retrieve their associated file names</strong>.  If this is the wrong approach, or if there's another way, please give me a push in the right direction.  Or, if I'm missing any details or something's unclear, please yell at me.</p>
  
  <p>Like the other question (linked above), I don't care about language (a C or C++ solution wouldn't hurt though), but I'd like for this to work on Windows XP.  Also, I require avoiding a kernel-mode driver.</p>
  
  <p>The problem I have with the original solution to this question is that if a file handle was opened a certain way, the <code>NtQueryObject</code> call may hang.  This is outlined on the SysInternals forums <a href="http://forum.sysinternals.com/howto-enumerate-handles_topic18892_post100895.html" rel="nofollow noreferrer">here</a>.</p>
  
  <p>According to the SysInternals forums, using CreateThread with a time-out has been the accepted solution, but doing that does not appear to allow the process to close properly every time.  Even while debugging this in Visual Studio, I am sometimes forced to reboot the computer.  Having to reboot my computer <em>sometimes</em> when I run this isn't the best option.</p>
  
  <p>Another accepted solution is skipping handles with a particular <code>GrantedAccess</code>.  The problem I have with that is that I miss too many of the handles to be useful given the <code>GrantedAccess</code> outlined on the above forum postings.</p>
  
  <p>Is anyone able to point me towards a solution to my problem?</p>
  
  <p>Thanks!</p>
  
  <p><strong>Edit</strong>: Sorry, I should have been more specific about my problem.  The <code>NtQuerySystemInformation</code> call will get me handles, the <code>NtQueryObject</code> called with <code>ObjectNameInformation</code> will hang on handles that are synchronous pipes (at least that's what people seem to say).  The example posted <a href="http://www.codeproject.com/KB/shell/OpenedFileFinder.aspx" rel="nofollow noreferrer">here</a> uses a kernel-mode driver to read the filename from the FILE_OBJECT -- but I want to avoid using a driver.  So yes, much like the SysInternals Handle utility, but I believe they use a driver as well, don't they?</p>
  
  <p><strong>Edit 2</strong>: This is sort of an academic interest, so use of the native API or other undocumented techniques that may be broken in future versions is not a concern.  Also, a <code>GrantedAccess</code> that just avoids objects that hang would be perfect.</p>
  
  <p><strong>Edit 3</strong>: My end goal is to just be able to see what files are currently open on the system.  If this is totally the wrong approach, another point in the right direction would be greatly appreciated.</p>
  
  <p><strong>Edit</strong>: This just needs to work on Windows XP as there are more elegant solutions for Vista+, so using undocumented functions really isn't a concern.</p>
  
  <p>Thanks again!</p>