什么是设置语法多个文件的扩展为searchPattern上Directory.GetFiles()?例如,使用.aspx和.ascx扩展名过滤掉文件.
// TODO: Set the string 'searchPattern' to only get files with
// the extension '.aspx' and '.ascx'.
var filteredFiles = Directory.GetFiles(path, searchPattern);
Run Code Online (Sandbox Code Playgroud)
更新:LINQ不是一个选项,它必须是一个searchPattern传入GetFiles,如问题中所指定的.
由于(至少在NTFS)在Windows文件系统不区分大小写,我想比较String fileA,以String fileB这样:
fileA.Equals(fileB, StringComparison.CurrentCultureIgnoreCase)
Run Code Online (Sandbox Code Playgroud)
那么问题就变成我应该使用哪种文化,默认的当前(ui?)文化是否足够?我似乎无法为此目的找到任何BCL方法.
假设我有两个字符串:a和b.为了比较a和被忽略大小写时是否具有相同的值,我总是使用:
// (Assume a and b have been verified not to be null)
if (a.ToLower() == b.ToLower())
Run Code Online (Sandbox Code Playgroud)
但是,使用Reflector,我在.NET Framework中已经看过几次:
// (arg three is ignoreCase)
if (string.Compare(a, b, true) == 0)
Run Code Online (Sandbox Code Playgroud)
我测试哪个更快,每次用我使用的字符串ToLower()节拍Compare().
是否有理由Compare()而不是ToLower()?有什么不同的CultureInfo?我在挠头.
我有一个名为的组合框comboFileTypes.里面是一个下拉列表,其中包含:
MP4
MOV
MKV
VOB
Run Code Online (Sandbox Code Playgroud)
按下按钮后,我有以下代码来扫描目录中的文件:
var files = Directory
.EnumerateFiles(sourceDIR.Text, "*.*", SearchOption.AllDirectories)
.Where(s =>
s.EndsWith(".mp4") ||
s.EndsWith(".mov") ||
s.EndsWith(".vob") ||
s.EndsWith(".MP4") ||
s.EndsWith(".MOV") ||
s.EndsWith(".VOB"));
Run Code Online (Sandbox Code Playgroud)
这是硬编码的.我希望WHERE从组合框中动态生成选项,以便用户可以根据需要添加其他类型的文件.(如果可能,也不区分大小写,否则我只会添加两种情况)
任何帮助,将不胜感激.
我想要扩展名为xls和xlsx的Excel文件,以及来自特定目录的FileInfo对象,所以我放下面的代码
System.IO.FileInfo[] files = null;
System.IO.DirectoryInfo dirInfo;
dirInfo = new System.IO.DirectoryInfo(this.tbFolderTo.Text);
string[] extensions = new[] { "*.xls", "*.xlsx" };
List<string> _searchPatternList = new List<string>(extensions);
List<string> fileList = new List<string>();
foreach (string ext in _searchPatternList)
{
foreach (string subFile in Directory.GetFiles(this.tbFolderTo.Text, ext))
{
fileList.Add(subFile);
}
}
foreach (string file in fileList)
{
this.lbFileNamesTo.Items.Add(file);
}
Run Code Online (Sandbox Code Playgroud)
但是通过使用像filexp2.xlsq或filexp.xlsa这样的虚假文件进行测试的问题,我在列表框中看到这些文件以显示找到的文件列表,在代码中我限制了对xls和xlsx的扩展我不知道为什么我看到结果中的这些文件
结果我没有看到我打的代码和这段代码之间的任何区别
System.IO.FileInfo[] files = null;
System.IO.DirectoryInfo dirInfo;
dirInfo = new System.IO.DirectoryInfo(this.tbFolderTo.Text);
files = dirInfo.GetFiles("*.xls*");
Run Code Online (Sandbox Code Playgroud)
感谢帮助
我有一个我无法处理的问题.
当我使用EnumerateFiles时,它不返回任何文件,我知道应该有超过3k的结果.
我的代码如下所示:
private IEnumerable<string> TestGetFiles(string path, params string[] exts)
{
var extsAsWildcards = exts.Select(x => "*." + x).ToArray();
return exts.Select(x => "*." + x) .SelectMany(x => Directory.EnumerateFiles(path, x));
}
Run Code Online (Sandbox Code Playgroud)
我从这里拿了这个代码:Git链接
没有解决方案可行,我需要这个解决方案用于多个扩展.对我有用的唯一方法是:
private IEnumerable<string> GetCurrentFontFiles(string item)
{
IEnumerable<string> files = Directory.EnumerateFiles(item + Path.DirectorySeparatorChar, "*.ttf", SearchOption.AllDirectories).ToArray();
return files;
}
Run Code Online (Sandbox Code Playgroud)
哪个有效,但仅适用于单个扩展.
请帮助我们,我不想使用速度非常慢的GetFiles(文件夹中有超过10k的文件).
EDIT1 我用这个过滤器调用方法:
string[] extensions = {"png"};
Run Code Online (Sandbox Code Playgroud)
EDIT2
我重现的例子:
private IEnumerable<string> TestGetFiles(string path, params string[] exts)
{
return exts.Select(x => "*." + x).SelectMany(x => Directory.EnumerateFiles(path, x));
}
private List<FileSizeList> GetPNGFiles(string …Run Code Online (Sandbox Code Playgroud)