如果项目中包含给定的用户输入,则C#返回搜索结果(项目)

问题描述 投票:0回答:2

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

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

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

这里是源代码:

    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# search directory items
2个回答
0
投票

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

//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;
}

0
投票

更改searchpattern = * 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);
© www.soinside.com 2019 - 2024. All rights reserved.