获取名称中不包含特定字符串的文件名 [英] Getting file name that doesn't contain particular string in its name
本文介绍了获取名称中不包含特定字符串的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设在一个目录中我有几个.xml文件。其中一些文件的名称中包含_schema字符串。我想获取那些不包含此特定字符串的文件名。
Suppose in a directory I have few .xml file. Some of these file has "_schema" string in it's name.I want to get those file name that doesn't contain this particular string.
Directory.GetFiles(pathOfTable,".xml")
代码返回所有.xml文件名
This code return all .xml file name
Directory.GetFiles(pathOfTable,"_schema.xml")
此代码返回带有_schema字符串的所有文件名
但是,我需要那些不包含此字符串的文件名
this code return all file name with "_schema" string
But, I need those file name that does not contain this string
推荐答案
Directory.GetFiles 不包含NOT选项。
解决方法是将所有文件放在文件夹中,然后用字符串操作查询每个文件
Directory.GetFiles does not contain a NOT option.
The way to go about it is to get all files in folder, then "query" each of them with a string operation
string[] fileEntries = Directory.GetFiles(targetDirectory);
foreach(string fileName in fileEntries)
{
if (!fileName.EndsWith("_schema.xml"))
actions...
}
这样,如果文件名不包含这样的字符串,那么if语句将会启动。
That way, if the file name does not contain such string, the "if" statement will kick in.
作为c#视图:
返回所有文件并使用列表处理算法或简单地使用linq进行任何后期处理
as c# view :
return all file and use list processing algorithms or simply use linq to any post processing
string[] fa = { "test_schemaoky", "farshad", "_schemafarshad", "saeidi" };
List<string> fl = new List<string>();
fl.AddRange(fa);
List<string> f2=fl.FindAll(itm => itm.IndexOf("_schema")==-1);
这篇关于获取名称中不包含特定字符串的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文