title: How do you extract the directory from a path including wildcards?
body: <p>Given paths like this:</p>
  
  <pre><code>C:\Temp\SomeDirectory\*.xml
  </code></pre>
  
  <p>I'd like to be able to distinguish the <code>*.xml</code> from <code>C:\Temp\SomeDirectory</code></p>
  
  <p>However, I do not want a path to a directory that does not have a trailing slash to return its parent directory.</p>
  
  <p>This means I want the following behaviour:</p>
  
  <pre><code>// Wildcard paths return directory
  C:\Temp\SomeDirectory\*.csv -&gt; C:\Temp\SomeDirectory
  
  // Trailing slash paths return full path
  C:\Temp\SomeDirectory\ -&gt; C:\Temp\SomeDirectory     
  
  // Non-trailing slash paths to a directory return full path
  C:\Temp\SomeDirectory -&gt; C:\Temp\SomeDirectory
  
  // Paths to a file return the directory
  C:\Temp\SomeDirectory\SomeFileThatExists.csv -&gt; C:\Temp\SomeDirectory
  
  // Paths to a file without an extension (that exists) return the directory
  C:\Temp\SomeDirectory\SomeFileThatExistsWithNoExt -&gt; C:\Temp\SomeDirectory
  
  // Paths to a non-existent path without a trailing slash are standard
  // Either always clip the trailing part, or always leave it in
  // (Can live with this one being C:\Temp\SomeDirectory)
  C:\Temp\SomeDirectory\NonExistentObject -&gt; C:\Temp\SomeDirectory\NonExistentObject 
  
  // Paths to a non-existent path with a trailing slash return the full path
  C:\Temp\SomeDirectory\NonExistentObject\ -&gt; C:\Temp\SomeDirectory\NonExistentObject
  
  // Paths to a non-existent path with a file extension return the directory
  C:\Temp\SomeDirectory\NonExistentFile.Ext -&gt; C:\Temp\SomeDirectory
  </code></pre>
  
  <p><em>(I am not fussed if the return value has a trailing slash or not, although the method I put below consistently does not return a trailing slash)</em></p>
  
  <p>My current code is something like this, and handles these cases:</p>
  
  <pre><code>public string GetDirectory(string path)
  {
      try
      {
          var f = new FileInfo(path); // Throws if invalid path, e.g. wildcards
  
          // Existent directory
          if (Directory.Exists(path))
          {
              // Full path must be a directory, so return full path
              // Ensure to add a trailing slash, as if it's missing it will return parent directory
              return Path.GetDirectoryName(path + '/');
          }
  
          // Non-existent directory (or ambiguous path without an extension or trailing slash)
          if (!System.IO.File.Exists(path) &amp;&amp; String.IsNullOrEmpty(Path.GetExtension(path)))
          {
              // Path is to a non-existent file (without an extension) or to a non-existent directory.
              // As the path does not exist we will standardise and treat it as a directory.
              return Path.GetDirectoryName(path + '/');
          }
  
          // Path is to a file, return directory
          return Path.GetDirectoryName(path);
      }
      catch (ArgumentException)
      {
          // For wildcards/invalid paths, return the directory
          // This maps C:\Dir\*.csv to C:\Dir
          // Also maps C:\Dir&amp;*A*#$!@&amp; to C:\
          return Path.GetDirectoryName(path);
      }
  }
  </code></pre>
  
  <p>Is there a better way to achieve this behaviour, or my ultimate goal of being able to get the "directory" from a path that may include wildcards?</p>