Search code examples
c#searchdirectoryitems

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


I've successfully created a class that contains a method which return a list of items paths within a given directory.

Problem is, the search criteria only return search results that initially include the user's input.

What I would like is that the search results will include any item that contains the user's input anywhere within the string of the item, not just if it starts with the input provided by the user.

Here is the source code:

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

How do I solve this issue? Thanks! :)


Solution

  • you can simply add a wildcard at the beginning as well like this :

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

    Moreover, you can use linq to shorten your entire method.

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

    Or even shorter by combining the dir_info into the 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;
    }
    

    Or even shorter, but I dont recommend doing this (im just having fine and having nothing to do right now)

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