Query Through File System Using .Net 4.0 Enumerable Collections

This article describes a new method introduced in .NET 4.0 using which developers can interact with file systems very quickly, especially for searching files and folders. In .NET Framework version 4, you can enumerate directories and files by using methods that return an enumerable collection. You can also use methods that return an enumerable collection of DirectoryInfo, FileInfo, or FileSystemInfo objects. In previous versions of the .NET Framework, you could only obtain arrays of these collections. Enumerable collections provide better performance than arrays. 

I created a small tool for searching folders and files inside a root path using .NET 4.0 ways. I have a folderbrowserdialog control to set a root path and 2 list boxes. First list box will display all folders and subfolders of the selected root path and second listbox will display the files directly under selected folder of first listbox. Once running the application, you will feel the smoothness of updated way of .NET 4.0 file system queries. In below section, I am explaining the code which has attached to this article.

Setting Root Path

You just browse a folder by clicking button "Set Search Directory" and I selected C drive.

imahe1.gif

Once above selection happening below code will be executed.

Loading Folders using Enumerable Collection Methods

if (folderBrowserDialog1.ShowDialog().Equals(DialogResult.OK))
{
    try
    {
        var dirNames = from dirs in Directory.EnumerateDirectories(folderBrowserDialog1.SelectedPath, "*.*", SearchOption.AllDirectories) select dirs;

        foreach (string dirName in dirNames)
        {
            listBox1.Items.Add(dirName);
        }
    }
    catch (UnauthorizedAccessException UAEx)
    {
        Console.WriteLine(UAEx.Message);
    }
    catch (PathTooLongException PathEx)
    {
        Console.WriteLine(PathEx.Message);
    }
}

As you can see we used a new method, EnumerateDirectories. The 1st parameter is the root directory. Second parameter is any filter applicable and I mentioned "*.*" i.e. all directories without any filter. 3rd parameter will decide whether you need full search or 1st level search and I decided to go with full search. You also need to take care about runtime security exception. I handled those exceptions and as of now we will get the result as shown below.

image2.gif

What you are seeing here is all folders under "C:\" listed inside 1st ListBox. Here for querying we used .NET 4.0 EnumerateDirectories(). Now our aim is the select each items [folder paths] inside 1st listbox and display the files directly under the selected item. I wrote the below logic for that.

Loading Files using Enumerable Collection Methods

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    listBox2.Items.Clear();
    try
    {
        var fileNames = from files in Directory.EnumerateFiles(listBox1.SelectedItem.ToString(), "*.*", SearchOption.TopDirectoryOnly) select files;
        foreach (string fileName in fileNames)
        {
            listBox2.Items.Add(fileName);
        }
    }
    catch (UnauthorizedAccessException UAEx)
    {
        Console.WriteLine(UAEx.Message);
    }
    catch (PathTooLongException PathEx)
    {
        Console.WriteLine(PathEx.Message);
    }
}


The main difference you can see that I used .NET 4.0 EnumerateFiles. The expected parameters here are same like in EnumerateDirectories. But here I made a difference in last parameter compare to EnumerateDirectories. I only need to search 1st level of files and no need to go with inner level. The second parameter here I mentioned as "*.*", for all files to list. But you can search only for your desired file types i.e. "*.txt" will search only text files. Below is the output with uploaded code.

image3.gif

Below is a list of such specific enumerable collection methods introduced as part of .NET 4.0. 

image4.gif