System.IO.FileInfo大小写不敏感搜索

本文关键字:搜索 大小写 IO FileInfo System | 更新日期: 2023-09-27 18:26:45

下面是我修改并最初从这里复制的一些代码:

http://msdn.microsoft.com/en-us/library/bb546159.aspx

我正在目录中搜索文件名,它运行良好,只是我不知道如何使其区分大小写

System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(startFolder);
 // This method assumes that the application has discovery permissions 
 // for all folders under the specified path.
 IEnumerable<System.IO.FileInfo> fileList = dir.GetFiles("*.*", System.IO.SearchOption.AllDirectories);
 //Create the query
 IEnumerable<System.IO.FileInfo> fileQuery =
     from file in fileList
     where file.Extension == ".pdf"
     where file.Name.Contains(searchString, StringComparison.OrdinalIgnoreCase)
     orderby file.Name
     select file;

此行包含错误:

where file.Name.Contains(searchString, StringComparison.OrdinalIgnoreCase)

错误为:

Error   1   'string' does not contain a definition for 'Contains' and the best extension method overload 'System.Linq.ParallelEnumerable.Contains<TSource>(System.Linq.ParallelQuery<TSource>, TSource, System.Collections.Generic.IEqualityComparer<TSource>)' has some invalid arguments  C:'Users'k5opjdjc'Documents'Visual Studio 2013'Projects'Test_File_Search'Test_File_Search'Controllers'SearchController.cs   30  23  Test_File_Search

我已经搜索了几个小时,找不到答案。如果有人能帮忙,我将不胜感激。

System.IO.FileInfo大小写不敏感搜索

这应该可以做到:

where file.Name.IndexOf(searchString, StringComparison.CurrentCultureIgnoreCase) >= 0

(当没有找到匹配的子字符串时,IndexOf返回-1)