Reputation: 91
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?
Upvotes: 9
Views: 1518
Reputation: 42991
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.
Upvotes: 11