DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 11.13 Searching for Directories or FilesUsing Wildcards

Problem

You are attempting to find one or more specific files or directories that might or might not exist within the current filesystem. The search might need to use wildcard characters in order to widen the search; for example, searching for all user mode dump files in a filesystem. These files have a .dmp extension.

Solution

There are several methods of obtaining this information. The first three methods return a string array containing the full path of each item. The next three methods return an object that encapsulates a directory, a file, or both.

The static GetFileSystemEntries method on the Directory class returns a string array containing the names of all files and directories within a single directory. For example, the following method retrieves a string array containing the names of all files and subdirectories in a particular directory, then displays them:

public void DisplayFilesDirs(string path)
{
    string[] items = Directory.GetFileSystemEntries(path);
    foreach (string item in items)
    {
        Console.WriteLine(item);
    }
}

The static GetDirectories method on the Directory class returns a string array containing the names of all directories within a single directory. For example, the following method retrieves a string array containing the names of all subdirectories in a particular directory, then displays them:

public void DisplayDirs(string path)
{
    string[] items = Directory.GetDirectories(path);
    foreach (string item in items)
    {
        Console.WriteLine(item);
    }

The static GetFiles method on the Directory class returns a string array containing the names of all files within a single directory. For example, the following method retrieves a string array containing the name of a file in a particular directory, then displays it:

public void DisplayFiles(string path)
{
    string[] items = Directory.GetFiles(path);
    foreach (string item in items)
    {
        Console.WriteLine(item);
    }
}

These next three methods return an object instead of simply a string. The GetFileSystemInfos method of the DirectoryInfo object returns a strongly typed array of FileSystemInfo objects (that is, of DirectoryInfo and FileInfo objects) representing the directories and files within a single directory. For example, the following code calls the GetFileSystemInfos method to retrieve an array of FileSystemInfo objects representing all the items in a particular directory, and then lists the Name property of each item to the console window:

public void DisplayFilesDirs(string path)
{
    DirectoryInfo mainDir = new DirectoryInfo(path);
    FileSystemInfo[] items = mainDir.GetFileSystemInfos( );
    foreach (FileSystemInfo item in items)
    {
        if (item is DirectoryInfo)
        {
            Console.WriteLine("DIRECTORY: " + ((DirectoryInfo)item).Name);
        }
        else if (item is FileInfo)
        {
            Console.WriteLine("FILE: " + item.Name);
        }
        else
        {
            Console.WriteLine("Unknown");
        }
    }
}

The GetDirectories instance method of the DirectoryInfo object returns an array of DirectoryInfo objects representing only subdirectories in a single directory. For example, the following code calls the GetDirectories method to retrieve an array of DirectoryInfo objects, then displays the Name property of each object to the console window:

public void DisplayDirs(string path)
{
    DirectoryInfo mainDir = new DirectoryInfo(path);
    DirectoryInfo[] items = mainDir.GetDirectories( );
    foreach (DirectoryInfo item in items)
    {
        Console.WriteLine("DIRECTORY: " + ((DirectoryInfo)item).Name);
    }
}

The GetFiles instance method of the FileInfo object returns an array of FileInfo objects representing only the files in a single directory. For example, the following code calls the GetFiles method to retrieve an array of FileInfo objects, then it displays the Name property of each object to the console window:

public void DisplayFiles(string path)
{
    DirectoryInfo mainDir = new DirectoryInfo(path);
    FileInfo[] items = mainDir.GetFiles( );
    foreach (FileInfo item in items)
    {
        Console.WriteLine("FILE: " + item.Name);
    }
}

There are several ways to obtain this information. The first three methods return a string representing the full path of the directory and/or file. The next three methods return an object that encapsulates a directory, a file, or both.

The static GetFileSystemEntries method on the Directory class returns all files and directories in a single directory that match pattern:

public void DisplayFilesDirs(string path, string pattern)
{
    string[] items = Directory.GetFileSystemEntries(path, pattern);
    foreach (string item in items)
    {
        Console.WriteLine(item);
    }
}

The static GetDirectories method on the Directory class returns only those directories in a single directory that match pattern:

public void DisplayDirs(string path, string pattern)
{
    string[] items = Directory.GetDirectories(path, pattern);
    foreach (string item in items)
    {
        Console.WriteLine(item);
    }
}

The static GetFiles method on the Directory class returns only those files in a single directory that match pattern:

public void DisplayFiles(string path, string pattern)
{
    string[] items = Directory.GetFiles(path, pattern);
    foreach (string item in items)
    {
        Console.WriteLine(item);
    }
}

These next three methods return an object instead of simply a string. The first instance method is GetFileSystemInfos, which returns both directories and files in a single directory that match pattern:

public void DisplayFilesDirs(string path, string pattern)
{
    DirectoryInfo mainDir = new DirectoryInfo(path);
    FileSystemInfo[] items = mainDir.GetFileSystemInfos(pattern);
    foreach (FileSystemInfo item in items)
    {
        if (item is DirectoryInfo)
        {
            Console.WriteLine("DIRECTORY: " + ((DirectoryInfo)item).Name);
        }
        else if (item is FileInfo)
        {
            Console.WriteLine("FILE: " + item.Name);
        }
        else
        {
            Console.WriteLine("Unknown");
        }
    }
}

The GetDirectories instance method returns only directories (contained in the DirectoryInfo object) in a single directory that match pattern:

public void DisplayDirs(string path, string pattern)
{
    DirectoryInfo mainDir = new DirectoryInfo(@"C:\TEMP ");
    DirectoryInfo[] items = mainDir.GetDirectories(pattern);
    foreach (DirectoryInfo item in items)
    {
        Console.WriteLine("DIRECTORY: " + ((DirectoryInfo)item).Name);
    }
}

The GetFiles instance method returns only file information (contained in the FileInfo object) in a single directory that match pattern:

public void DisplayFiles(string path, string pattern)
{
    DirectoryInfo mainDir = new DirectoryInfo(@"C:\TEMP ");
    FileInfo[] items = mainDir.GetFiles(pattern);
    foreach (FileInfo item in items)
    {
        Console.WriteLine("FILE: " + item.Name);
    }
}

Discussion

If you need just an array of strings containing paths to both directories and files, you can use the static method Directory.GetFileSystemEntries. The string array returned does not include any information about whether an individual element is a directory or a file. Each string element contains the entire path to either a directory or file contained within the specified path.

To quickly and easily distinguish between directories and files, use the Directory.GetDirectories and Directory.GetFiles static methods. These methods return arrays of directory names and filenames. These methods return an array of string objects. Each element contains the full path to the directory or file.

Returning a string is fine if you do not need any other information about the directory or file returned to you or if you are going to need more information for only one of the files returned. It is more efficient to use the static methods to get the list of filenames and just retrieve the FileInfo for the ones you need than to have all of the FileInfos constructed for the directory that the instance methods will do. If you are going to have to access attributes, lengths, or times on every one of the files, you should consider using the instance methods described here.

The instance method GetFileSystemInfos returns an array of strongly typed FileSystemInfo objects. (The FileSystemInfo object is the base class to the DirectoryInfo and FileInfo objects.) Therefore, you can test whether the returned type is a DirectoryInfo or FileInfo object using the is or as keywords. Once you know what subclass this object really is, you can cast it to that type and begin using it.

To get only DirectoryInfo objects, use the overloaded GetDirectories instance method. To get only Fileinfo objects, use the overloaded GetFiles instance method. These methods return an array of DirectoryInfo and FileInfo objects respectively; each element of which encapsulates a directory or file.

See Also

See the "DirectoryInfo Class," "FileInfo Class," and "FileSystemInfo Class" topics in the MSDN documentation.

    [ Team LiB ] Previous Section Next Section