C# return 搜索结果(项)如果给定的用户输入包含在该项中

C# return search results (items) if a given user input is contained within that item

我已经成功创建了一个 class,其中包含一个方法,该方法 return 给定目录中的项目路径列表。

问题是,搜索条件仅 return 最初包含用户输入的搜索结果。

我想要的是,搜索结果将包括在项目字符串中任意位置包含用户输入的任何项目,而不仅仅是以用户提供的输入开头的项目。

这里是源代码:

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

更改搜索模式 = *userinput*

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