首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >System.IO.FileInfo不区分大小写搜索

System.IO.FileInfo不区分大小写搜索
EN

Stack Overflow用户
提问于 2014-10-16 20:14:31
回答 1查看 2.3K关注 0票数 3

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

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

我在一个目录中搜索文件名,它工作得很好,但我想不出如何使它成为不敏感的

代码语言:javascript
复制
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;

这一行包含错误:

代码语言:javascript
复制
where file.Name.Contains(searchString, StringComparison.OrdinalIgnoreCase)

错误是:

代码语言:javascript
复制
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

我已经找了几个小时了,找不到答案。如果有人能帮忙,我会非常感激的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-16 20:31:49

这应该能起作用:

代码语言:javascript
复制
where file.Name.IndexOf(searchString, StringComparison.CurrentCultureIgnoreCase) >= 0

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26413090

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档