Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

DirectoryInfo.GetFileSystemInfos Method (String)

Retrieves an array of strongly typed FileSystemInfo objects representing the files and subdirectories matching the specified search criteria.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)

public FileSystemInfo[] GetFileSystemInfos(
	string searchPattern
)

Parameters

searchPattern
Type: System.String

The search string, such as "System*", used to search for all directories beginning with the word "System".

Return Value

Type: System.IO.FileSystemInfo[]
An array of strongly typed FileSystemInfo objects matching the search criteria.

ExceptionCondition
ArgumentException

searchPattern contains invalid characters. To determine the invalid characters, use the GetInvalidPathChars method.

ArgumentNullException

searchPattern is null.

DirectoryNotFoundException

The specified path is invalid, such as being on an unmapped drive.

SecurityException

The caller does not have the required permission.

This method is not recursive.

For subdirectories, the FileSystemInfo objects returned by this method can be cast to the derived class DirectoryInfo. Use the FileAttributes value returned by the FileSystemInfo.Attributes property to determine whether the FileSystemInfo represents a file or a directory.

Wild cards are permitted. For example, the searchPattern string "*t" searches for all directory names in path ending with the letter "t". The searchPattern string "s*" searches for all directory names in path beginning with the letter "s".

The string ".." can only be used in searchPattern if it is specified as a part of a valid directory name, such as in the directory name "a..b". It cannot be used to move up the directory hierarchy. If there are no files or directories, or no files or directories that match the searchPattern string in the DirectoryInfo, this method returns an empty array.

For a list of common I/O tasks, see Common I/O Tasks.

The following example counts the files and directories that match the specified search pattern.

using System;
using System.IO;

class DirectoryFileCount
{

    static long files = 0;
    static long directories = 0;


    static void Main()
    {
        try
        {
            Console.WriteLine("Enter the path to a directory:");

            string directory = Console.ReadLine();

            Console.WriteLine("Enter a search string (for example *p*):");

            string searchString = Console.ReadLine();

            // Create a new DirectoryInfo object.
            DirectoryInfo dir = new DirectoryInfo(directory);

            if (!dir.Exists)
            {
                throw new DirectoryNotFoundException("The directory does not exist.");
            }

            // Call the GetFileSystemInfos method.
            FileSystemInfo[] infos = dir.GetFileSystemInfos(searchString);

            Console.WriteLine("Working...");

            // Pass the result to the ListDirectoriesAndFiles 
            // method defined below.
            ListDirectoriesAndFiles(infos, searchString);

            // Display the results to the console. 
            Console.WriteLine("Directories: {0}", directories);
            Console.WriteLine("Files: {0}", files);

        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
        finally
        {

            Console.ReadLine();
        }
    }

    static void ListDirectoriesAndFiles(FileSystemInfo[] FSInfo, string SearchString)
    {
        // Check the parameters. 
        if (FSInfo == null)
        {
            throw new ArgumentNullException("FSInfo");
        }
        if (SearchString == null || SearchString.Length == 0)
        {
            throw new ArgumentNullException("SearchString");
        }

        // Iterate through each item. 
        foreach (FileSystemInfo i in FSInfo)
        {
            // Check to see if this is a DirectoryInfo object. 
            if (i is DirectoryInfo)
            {
                // Add one to the directory count.
                directories++;

                // Cast the object to a DirectoryInfo object.
                DirectoryInfo dInfo = (DirectoryInfo)i;

                // Iterate through all sub-directories.
                ListDirectoriesAndFiles(dInfo.GetFileSystemInfos(SearchString), SearchString);
            }
            // Check to see if this is a FileInfo object. 
            else if (i is FileInfo)
            {
                // Add one to the file count.
                files++;

            }

        }
    }
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

Show:
© 2014 Microsoft