【发布时间】:2015-11-14 13:37:08
【问题描述】:
我在 C# 中有一个方法可以通过这种方式获取目录中的文件:
FileInfo[] fileInfo = new DirectoryInfo(mypath).GetFiles();
目录中的某些文件不是我们需要处理的文件(唯一知道的方法是通过其内容,而不是文件扩展名),因此我们希望将它们从 FileInfo 列表中删除(而不是从磁盘中) .
我正在寻找一种简单的方法来排除 FileInfo 数组中的文件,但似乎没有办法。
这是检查我们只需要在用户选择的目录中的文件的整个代码:
int number_of_files = fileInfo.Length;
for (int i = 0; i < number_of_files ; ++i)
{
string file= fileInfo[i].FullName;
BinaryReader br = new BinaryReader(new FileStream(file, FileMode.Open, FileAccess.Read), Encoding.ASCII);
byte[] preamble = new byte[132];
br.Read(preamble, 0, 132);
if (preamble[128] != 'D' || preamble[129] != 'I' || preamble[130] != 'C' || preamble[131] != 'M')
{
if (preamble[0] + preamble[1] != 0008)
{
return; //Rather than return, remove the file in question from the list....
}
}
br.Dispose();
}
任何想法我该怎么做?
【问题讨论】:
-
那么你如何表达你不需要的那些?
-
我添加了更多信息,不取决于文件扩展名,重要的是文件内容