如何检查,如果文件名包含字符串在C#字符串、文件名

由网友(努力奋斗)分享简介:我有一个指定的文件夹myfileone myfiletwo myfilethree 我如何检查是否文件myfilethree是present。How can I check if file "myfilethree" is present.我的意思是有另一种方法比其他 IsFileExist()的方法,即像文件名...

我有一个指定的文件夹

myfileone myfiletwo myfilethree

我如何检查是否文件myfilethree是present。

How can I check if file "myfilethree" is present.

我的意思是有另一种方法比其他 IsFileExist()的方法,即像文件名中包含字符串三?

I mean is there another method other than IsFileExist() method, i.e like filename contains substring "three"?

推荐答案

子字符串:

bool contains  = Directory.EnumerateFiles(path).Any(f => f.Contains("three"));

不区分大小写字符串:

Case-insensitive substring:

bool contains  = Directory.EnumerateFiles(path).Any(f => f.IndexOf("three", StringComparison.OrdinalIgnoreCase) > 0);

区分大小写的比较:

Case-insensitive comparison:

bool contains  = Directory.EnumerateFiles(path).Any(f => String.Equals(f, "myfilethree", StringComparison.OrdinalIgnoreCase));

获取文件名匹配通配符标准:

Get file names matching a wildcard criteria:

IEnumerable<string> files = Directory.EnumerateFiles(path, "three*.*"); // lazy file system lookup

string[] files = Directory.GetFiles(path, "three*.*"); // not lazy
阅读全文

相关推荐

最新文章