Directory.GetFiles()不是用&QUOT模式工作;" [英] Directory.GetFiles() not working with a pattern of "."

查看:98
本文介绍了Directory.GetFiles()不是用&QUOT模式工作;"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,当我添加下面一行到我的WPF应用程序的一些奇怪的问题。

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;

}



的事情是,在框架3.5 上面的方法不起作用,即使不是错误,但如果我将其更改为框架4.5
它的作品!另外,如果我使用的框架3.5 并把它变成ConsolApp这样

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?

推荐答案

我想这一点,得到了相同的结果。钻孔与ReSharper的API源代码显示,.NET 3.5和Directory.GetFiles的4.5版本是完全不同的。

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.

在特别是.NET 4.5版本包含此功能(和.NET 3.5没有):

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;
}



这解释了为什么'。'搜索模式的.NET 4.5,但工程而不是3.5。

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

您应该使用*或*。*的兼容性。

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

这篇关于Directory.GetFiles()不是用&QUOT模式工作;"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆