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

Returns an array of strongly typed FileSystemInfo entries representing all the files and subdirectories in a directory.

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

public FileSystemInfo[] GetFileSystemInfos ()
public FileSystemInfo[] GetFileSystemInfos ()
public function GetFileSystemInfos () : FileSystemInfo[]

Return Value

An array of strongly typed FileSystemInfo entries.

Exception typeCondition

DirectoryNotFoundException

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

If there are no files or directories in the DirectoryInfo, this method returns an empty array. 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.

The following table lists examples of other typical or related I/O tasks.

To do this...

See the example in this topic...

Create a text file.

How to: Write Text to a File

Write to a text file.

How to: Write Text to a File

Read from a text file.

How to: Read Text from a File

Rename or move a directory.

Directory.Move

DirectoryInfo.MoveTo

Rename or move a file.

File.Move

FileInfo.MoveTo

Delete a file.

File.Delete

FileInfo.Delete

Delete a directory.

Directory.Delete

DirectoryInfo.Delete

Create a directory.

CreateDirectory

Directory

Create a subdirectory.

CreateSubdirectory

See the files in a directory.

Name

See the subdirectories of a directory.

GetDirectories

GetDirectories

Find the size of a directory.

Directory

Determine if a file exists.

Exists

Determine if a directory exists.

Exists

Set file attributes.

SetAttributes

Get file attributes.

GetAttributes

The following example counts the files and directories under the specified directory.

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

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

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

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

            // 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)
    {
        // Check the FSInfo parameter.
        if (FSInfo == null)
        {
            throw new ArgumentNullException("FSInfo");
        }

        // 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());
            }
            // Check to see if this is a FileInfo object.
            else if (i is FileInfo)
            {
                // Add one to the file count.
                files++;

            }

        }
    }
}

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft