Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Directory.GetFiles() not working with a pattern of "."

Tags:

c#

.net

directory

I have some weird problem when I'm adding the following line into my WPF App.

private void button1_Click(object sender, RoutedEventArgs e)
{

    foreach(string files in Directory.GetFiles(path,".",SearchOption.TopDirectoryOnly))
      tb_FileBrowse.Text = files;

}

The thing is that in FrameWork 3.5 the above method does nothing, not even an error, but if I change it to FrameWork 4.5 it Works!. Also if I'm using Framework 3.5 and change it into ConsolApp like this

foreach (string files in Directory.GetFiles(path, ".", SearchOption.TopDirectoryOnly))
{
   Console.WriteLine("{0}",files);
}

The code give some results.

Does anyone have the same problem?

like image 811
John Evan Avatar asked Feb 16 '13 21:02

John Evan


People also ask

Can you call directory GetFiles () with multiple filters?

[ VB.NET ] Net Framework will probably have Directory. getFiles method that supports multiple filters.

What is directory GetFiles?

GetFiles(String, String, SearchOption) Returns the names of files (including their paths) that match the specified search pattern in the specified directory, using a value to determine whether to search subdirectories.


1 Answers

I tried this and got the same results. Drilling into the API source code with Resharper reveals that the .NET 3.5 and 4.5 versions of Directory.GetFiles are totally different.

In particular the .NET 4.5 version contains this function (and .NET 3.5 doesn't):

private static string NormalizeSearchPattern(string searchPattern)
{
  string searchPattern1 = searchPattern.TrimEnd(Path.TrimEndChars);
  if (searchPattern1.Equals("."))
    searchPattern1 = "*";
  Path.CheckSearchPattern(searchPattern1);
  return searchPattern1;
}

Which explains why a search pattern of '.' works on .NET 4.5 but not on 3.5.

You should use '*' or '*.*' for compatibility.

like image 108
Phil Avatar answered Nov 07 '22 12:11

Phil