gpt4 book ai didi

c# - System.IO.FileInfo 不区分大小写的搜索

转载 作者:太空宇宙 更新时间:2023-11-03 10:38:36 26 4
gpt4 key购买 nike

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

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

我已经搜索了几个小时,但找不到答案。如果有人可以提供帮助,我将不胜感激。

最佳答案

这应该可以解决问题:

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

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

关于c# - System.IO.FileInfo 不区分大小写的搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26413090/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com