title: Filtering file names: getting *.abc without *.abcd, or *.abcde, and so on
body: <p>Directory.GetFiles(LocalFilePath, searchPattern);</p>
  
  <p>MSDN Notes:</p>
  
  <blockquote>
    <p>When using the asterisk wildcard character in a searchPattern, such as "<em>.txt", the matching behavior when the extension is exactly three characters long is different than when the extension is more or less than three characters long. A searchPattern with a file extension of exactly three characters returns files having an extension of three or more characters, where the first three characters match the file extension specified in the searchPattern. A searchPattern with a file extension of one, two, or more than three characters returns only files having extensions of exactly that length that match the file extension specified in the searchPattern. When using the question mark wildcard character, this method returns only files that match the specified file extension. For example, given two files, "file1.txt" and "file1.txtother", in a directory, a search pattern of "file?.txt" returns just the first file, while a search pattern of "file</em>.txt" returns both files.</p>
  </blockquote>
  
  <p>The following list shows the behavior of different lengths for the searchPattern parameter:</p>
  
  <ul>
  <li><p><code>*.abc</code> returns files having an extension of <code>.abc</code>, <code>.abcd</code>, <code>.abcde</code>, <code>.abcdef</code>, and so on.</p></li>
  <li><p><code>*.abcd</code> returns only files having an extension of <code>.abcd</code>.</p></li>
  <li><p><code>*.abcde</code> returns only files having an extension of <code>.abcde</code>.</p></li>
  <li><p><code>*.abcdef</code> returns only files having an extension of <code>.abcdef</code>. </p></li>
  </ul>
  
  <p>With the <code>searchPattern</code> parameter set to <code>*.abc</code>, how can I return files having an extension of <code>.abc</code>, not <code>.abcd</code>, <code>.abcde</code> and so on?</p>
  
  <p>Maybe this function will work:</p>
  
  <pre><code>    private bool StriktMatch(string fileExtension, string searchPattern)
      {
          bool isStriktMatch = false;
  
          string extension = searchPattern.Substring(searchPattern.LastIndexOf('.'));
  
          if (String.IsNullOrEmpty(extension))
          {
              isStriktMatch = true;
          }
          else if (extension.IndexOfAny(new char[] { '*', '?' }) != -1)
          {
              isStriktMatch = true;
          }
          else if (String.Compare(fileExtension, extension, true) == 0)
          {
              isStriktMatch = true;
          }
          else
          {
              isStriktMatch = false;
          }
  
          return isStriktMatch;
      }
  </code></pre>
  
  <p>Test Program:</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          string[] fileNames = Directory.GetFiles("C:\\document", "*.abc");
  
          ArrayList al = new ArrayList();
  
          for (int i = 0; i &lt; fileNames.Length; i++)
          {
              FileInfo file = new FileInfo(fileNames[i]);
              if (StriktMatch(file.Extension, "*.abc"))
              {
                  al.Add(fileNames[i]);
              }
          }
  
          fileNames = (String[])al.ToArray(typeof(String));
  
          foreach (string s in fileNames)
          {
              Console.WriteLine(s);
          }
  
          Console.Read();
      }
  </code></pre>
  
  <p>Anybody else better solution?</p>