我的代码显示了富文本框中目录中所有.dll文件的名称。我如何能够过滤/隐藏我指定为不重要的所有文件名并保留其余文件名?
示例:
实际目录包含:1.dll,2.dll,3.dll
Rich Textbox显示:1.dll,3.dll因为2.dll在代码中被指定为不重要。
我目前拥有的代码显示所有文件。
DirectoryInfo r = new DirectoryInfo(@"E:\SteamLibrary\steamapps\common\Grand Theft Auto V");
FileInfo[] rFiles = r.GetFiles("*.dll");
string rstr = "";
foreach (FileInfo rfile in rFiles)
{
rstr = rstr + rfile.Name + " ";
}
string strfinalR;
strfinalR = richTextBox3.Text + rstr;
richTextBox3.Text = (strfinalR);
答案 0 :(得分:1)
只需制作黑名单:
string[] blacklist = new string[] { "2", "1337" };
并过滤foreach
foreach(FileInfo rFile in rFiles)
{
if(blacklist.Any(bl => rFile.Name.Contains(bl)))
continue;
// your code
}
或从r
r.GetFiles("*.dll").Where(file => !blacklist.Any( type => file.Name.Contains(type))).ToArray();
答案 1 :(得分:0)
一种方法是创建要忽略的文件列表,然后获取除忽略列表中的文件之外的所有文件。像这样:
//ignore list
List<string> filesToIgnore = new List<string>() { "2.dll", "some_other.dll" };
//get files and filter them
DirectoryInfo r = new DirectoryInfo(@"E:\SteamLibrary\steamapps\common\Grand Theft Auto V");
List<FileInfo> rFiles = r.GetFiles("*.dll").Where(x => !filesToIgnore.Contains(x.Name)).ToList();
//your existing code follows
string rstr = "";
foreach (FileInfo rfile in rFiles)
{
rstr = rstr + rfile.Name + " ";
}
string strfinalR;
strfinalR = richTextBox3.Text + rstr;
richTextBox3.Text = (strfinalR);