title: How to find out if a directory exists on Delphi XE2 *correctly*?
body: <p>I simply need to check if a directory exists! But if the directory is "E:\Test" where E: is CD/DVD drive and there is no disk inserted on it, I see the following Delphi and Windows issues.  </p>
  
  <p><strong>The first method:</strong></p>
  
  <pre><code>function DirExists(Name: string): Boolean;
  var
    Code: Integer;
  begin
    Code := GetFileAttributesW(PChar(Name));
    Result := (Code &lt;&gt; -1) and (FILE_ATTRIBUTE_DIRECTORY and Code &lt;&gt; 0);
  end;
  </code></pre>
  
  <p>It gives <code>Range Check Error</code>. I can't use <code>{$RANGECHECKS OFF}</code>, <code>{$RANGECHECKS ON}</code> blocks because:</p>
  
  <ol>
  <li>It breaks the current state of <code>$RANGECHECKS</code> option.</li>
  <li>We will see another system error <code>Drive is not ready</code> instead <code>Range Check Error</code>. But I simply need to check if directory exists without any error dialogs for user.</li>
  </ol>
  
  <p><strong>The second method:</strong></p>
  
  <pre><code>if DirectoryExists(Name, True) then ...
  </code></pre>
  
  <p>This function returns <code>True</code> for non-existing <code>E:\Test</code> directory on empty CD/DVD drive. So can't use it because it works incorrectly.</p>
  
  <p>But then, how to find out if a directory exists?</p>
  
  <p><em>P.S. I think the error exists with any CD/DVD drive. But I am using Windows 7 x64 on VMWare Fusion 5 under Mac OS X 10.8.4 with external CD/DVD drive.</em></p>