Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#-检查文件名是否以某个单词结尾_C#_File - Fatal编程技术网

C#-检查文件名是否以某个单词结尾

C#-检查文件名是否以某个单词结尾,c#,file,C#,File,您好,我想知道如何在C#中验证文件名,以检查它是否以某个单词结尾(不仅包含,而且位于文件名的末尾) e、 g我想修改以后缀Supplier结尾的某些文件,所以我想验证每个文件,以测试它是否以Supplier结尾,这样LondonSupplier.txt、ManchesterSupplier.txt和BirminghamSupplier.txt都将被验证并返回true,但ManchesterSuppliers.txt不会 这可能吗?我知道您可以验证文件名,以便在文件名中的任何位置测试某个单词,但是

您好,我想知道如何在C#中验证文件名,以检查它是否以某个单词结尾(不仅包含,而且位于文件名的末尾)

e、 g我想修改以后缀Supplier结尾的某些文件,所以我想验证每个文件,以测试它是否以Supplier结尾,这样LondonSupplier.txt、ManchesterSupplier.txt和BirminghamSupplier.txt都将被验证并返回true,但ManchesterSuppliers.txt不会

这可能吗?我知道您可以验证文件名,以便在文件名中的任何位置测试某个单词,但是否可以执行我的建议?

尝试:

Path.GetFileNameWithoutExtension(path).EndsWith("Supplier")
if(myFileName.EndsWith(“任意”)) { //做事
}

通过使用
System.IO.Path.GetFileNameWithoutExtension(string)
方法,可以从字符串中提取文件名(无扩展名)。例如,使用字符串C:\svn\trunk\MySourceFile.cs调用它将返回字符串MySourceFile。在此之后,您可以使用
String.EndsWith
方法查看文件名是否符合您的标准。

Linq解决方案:

    var result = FilePaths.Where(name => Path.GetFileNameWithoutExtension(name).EndsWith("Supplier"))
                      .Select(name => name).ToList();
假设filepath是包含所有路径的列表