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?
[ VB.NET ] Net Framework will probably have Directory. getFiles method that supports multiple filters.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With