如何检查文件名是否包含C#中的子字符串

sre*_*sad 11 .net c#

我有一个名为文件的文件夹

  1. myfileone
  2. myfiletwo
  3. myfilethree

如何检查文件"myfilethree"是否存在.

我的意思是除了方法之外还有另一种IsFileExist()方法,即像filename包含子串"三"?

aba*_*hev 22

子:

bool contains  = Directory.EnumerateFiles(path).Any(f => f.Contains("three"));
Run Code Online (Sandbox Code Playgroud)

不区分大小写的子字符串:

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

不区分大小写的比较:

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

获取与通配符条件匹配的文件名:

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

string[] files = Directory.GetFiles(path, "three*.*"); // not lazy
Run Code Online (Sandbox Code Playgroud)


Ray*_*Ray 5

如果我正确理解你的问题,你可以做类似的事情

Directory.GetFiles(directoryPath, "*three*")

或者

Directory.GetFiles(directoryPath).Where(f => f.Contains("three"))

three这两个都会为您提供其中所有文件的所有名称。