首页 > 解决方案 > 如果给定的用户输入包含在该项目中,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;
        }

我该如何解决这个问题?谢谢!:)

标签: c#searchdirectoryitems

解决方案


您可以像这样简单地在开头添加通配符:

//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();

推荐阅读