c# - 如果给定的用户输入包含在该项目中,C# 返回搜索结果(项目)
问题描述
我已经成功创建了一个类,该类包含一个返回给定目录中项目路径列表的方法。
问题是,搜索条件只返回最初包含用户输入的搜索结果。
我想要的是搜索结果将包括在项目字符串中的任何位置包含用户输入的任何项目,而不仅仅是它以用户提供的输入开头。
这是源代码:
public class SearchManager
{
//Returns a list of items that contain the user's input within a given path
public static List<string> SearchResults(string current_path, string user_input)
{
//Here we access the class 'DirectoryInfo'
DirectoryInfo dir_info = new DirectoryInfo(current_path);
//This array stores all the directories found within the current path/directory
DirectoryInfo[] directories = dir_info.GetDirectories(user_input + "*", SearchOption.TopDirectoryOnly);
//This array stores all the files found within the current path/directory
FileInfo[] files = dir_info.GetFiles(user_input + "*", SearchOption.TopDirectoryOnly);
//This list will store both directories and files found within the search result
List<string> ItemsFound = new List<string>();
//Here we loop through each item within both arrays in order to populate that list of items
//Adds all the given paths of the directories
foreach (DirectoryInfo dir in directories)
ItemsFound.Add(dir.FullName);
//Adds all the given paths of the files
foreach (FileInfo file in files)
ItemsFound.Add(file.FullName);
//Here, we return a list of items data, from our search result to populate the data grid
return ItemsFound;
}
我该如何解决这个问题?谢谢!:)
解决方案
您可以像这样简单地在开头添加通配符:
//This array stores all the directories found within the current path/directory
DirectoryInfo[] directories = dir_info.GetDirectories("*" + user_input + "*", SearchOption.TopDirectoryOnly);
//This array stores all the files found within the current path/directory
FileInfo[] files = dir_info.GetFiles("*" + user_input + "*", SearchOption.TopDirectoryOnly);
此外,您可以使用 linq 来缩短整个方法。
public static List<string> SearchResults(string current_path, string user_input)
{
//Here we access the class 'DirectoryInfo'
DirectoryInfo dir_info = new DirectoryInfo(current_path);
//This array stores all the directories found within the current path/directory
DirectoryInfo[] directories = dir_info.GetDirectories("*" + user_input + "*", SearchOption.TopDirectoryOnly);
//This array stores all the files found within the current path/directory
FileInfo[] files = dir_info.GetFiles("*" + user_input + "*", SearchOption.TopDirectoryOnly);
//This list will store both directories and files found within the search result
List<string> ItemsFound = new List<string>(); ;
ItemsFound.AddRange(directories.Select(x => x.FullName));
ItemsFound.AddRange(files.Select(x => x.FullName));
//Here, we return a list of items data, from our search result to populate the data grid
return ItemsFound;
}
或者通过将 dir_info 组合到 linq 中甚至更短:
public static List<string> SearchResults(string current_path, string user_input)
{
//Here we access the class 'DirectoryInfo'
DirectoryInfo dir_info = new DirectoryInfo(current_path);
//This list will store both directories and files found within the search result
List<string> ItemsFound = new List<string>();
ItemsFound.AddRange(dir_info.GetDirectories("*" + user_input + "*", SearchOption.TopDirectoryOnly).Select(x => x.FullName));
ItemsFound.AddRange(dir_info.GetFiles("*" + user_input + "*", SearchOption.TopDirectoryOnly).Select(x => x.FullName));
//Here, we return a list of items data, from our search result to populate the data grid
return ItemsFound;
}
甚至更短,但我不建议这样做(我现在很好,无事可做)
public static List<string> SearchResults(string current_path, string user_input)
=> (new DirectoryInfo(current_path)).GetFiles("*" + user_input + "*", SearchOption.TopDirectoryOnly).Select(x => x.FullName).Concat(((new DirectoryInfo(current_path)).GetDirectories("*" + user_input + "*", SearchOption.TopDirectoryOnly).Select(x => x.FullName))).ToList();
推荐阅读
- firebase - 如何通过 Flutter 中的方法获取 Firestore 数据
- html - 如何减少链接的高度并保持文本高度对齐?
- javascript - 我的自动增量值不适用于我附加的文本框
- flutter - 无法从飞镖列表中访问单个元素
- python - 为什么我不能使用 pip 和 pip3 但 python3 -m 安装任何软件包?
- node.js - stdio“继承”与管道
- visual-studio-code - vscode - Visual Studio 代码 - 括号前的点/省略号
- r - 我可以从闪亮的网站下载我闪亮的应用程序的 app.R 脚本和数据吗?
- python - 在 Dataflow 管道中调用 Bigquery 存储过程
- c# - 如何在 MRTK 中以编程方式分配可交互事件