Export (0) Print
Expand All

DirectoryInfo.GetFileSystemInfos Method ()

.NET Framework 1.1

Returns an array of strongly typed FileSystemInfo entries listing all the files and directories.

[Visual Basic]
Overloads Public Function GetFileSystemInfos() As FileSystemInfo()
[C#]
public FileSystemInfo[] GetFileSystemInfos();
[C++]
public: FileSystemInfo* GetFileSystemInfos() [];
[JScript]
public function GetFileSystemInfos() : FileSystemInfo[];

Return Value

An array of strongly typed FileSystemInfo entries.

Exceptions

Exception Type Condition
DirectoryNotFoundException The path is invalid, such as being on an unmapped drive.

Remarks

If there are no files or directories in the DirectoryInfo, this method returns an empty array.

For an example of using this method, see the Example section below. 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. Writing Text to a File
Write to a text file. Writing Text to a File
Read from a text file. Reading Text from a File
Copy a directory. Directory
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

Example

[Visual Basic, C#, C++] The following example counts the files and directories in a path.

[Visual Basic] 
Imports System
Imports System.IO

Public Class Test
    Public Shared Sub Main()
        ' Specify the directories you want to manipulate.
        Dim di As DirectoryInfo = New DirectoryInfo("c:\")

        Try
            'Get only subdirectories that contain the letter "p."
            Dim dirs As FileSystemInfo() = di.GetDirectories("*p*")
            Console.WriteLine("Number of directories with a p: {0}", dirs.Length)
            Dim diNext As DirectoryInfo
            'Count all the files in each subdirectory that contain the letter "e."
            For Each diNext In dirs
                Console.WriteLine("The number of files and directories " + _
                 "in {0} is {1}", diNext, _
                 diNext.GetFileSystemInfos().Length)
            Next
        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.ToString())
        End Try
    End Sub
End Class

[C#] 
using System;
using System.IO;

class Test 
{
    public static void Main() 
    {
        try 
        {
            DirectoryInfo di = new DirectoryInfo(@"c:\");
            // Get only subdirectories that contain the letter "p."
            FileSystemInfo[] dirs = di.GetDirectories("*p*");
            Console.WriteLine("Number of directories with a p: {0}", dirs.Length);
            // Count all the files in each subdirectory that contain the letter "e."
            foreach (DirectoryInfo diNext in dirs) 
            {
                Console.WriteLine("The number of files and directories " +
                    "in {0} is {1}", diNext, 
                    diNext.GetFileSystemInfos().Length);
            }
        } 

        catch (Exception e) 
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;

int main() {
    try {
        DirectoryInfo* di = new DirectoryInfo(S"c:\\");
        // Get only subdirectories that contain the letter "p."
        FileSystemInfo* dirs[] = di->GetDirectories(S"*p*");
        Console::WriteLine(S"Number of directories with a p: {0}", __box(dirs->Length));
        // Count all the files in each subdirectory that contain the letter "e."
        Collections::IEnumerator* myEnum = dirs->GetEnumerator();
        while (myEnum->MoveNext()) {
            DirectoryInfo* diNext = __try_cast<DirectoryInfo*>(myEnum->Current);
            Console::WriteLine(S"The number of files and directories in {0} is {1}", diNext, 
                __box(diNext->GetFileSystemInfos()->Length));
        }
    } catch (Exception* e) {
        Console::WriteLine(S"The process failed: {0}", e);
    }
}

[Visual Basic, C#, C++] The following example demonstrates listing the contents of a directory and sorting them by size. If the directory has subdirectories, they are listed first.

[Visual Basic] 
Imports System
Imports System.IO
Imports System.Collections

Class Test

    Dim sc As IComparer = New SizeComparer()
    Private Sub ProcessDirectory(ByVal dir As DirectoryInfo)
        Dim infos As FileSystemInfo() = dir.GetFileSystemInfos()
        Array.Sort(infos, sc)
        Dim f As FileSystemInfo
        For Each f In infos
            If TypeOf f Is DirectoryInfo Then
                ProcessDirectory(CType(f, DirectoryInfo))
            Else
                If TypeOf f Is FileInfo Then
                    ProcessFile(CType(f, FileInfo))
                End If
            End If
        Next f
    End Sub 'ProcessDirectory

    Private Sub ProcessFile(ByVal f As FileInfo)
        Console.WriteLine("{0,16:N0} {1}", f.Length, f.FullName)
    End Sub 'ProcessFile      

    Class SizeComparer
        Implements IComparer
        Public Function Compare(ByVal info1 As Object, ByVal info2 As Object) As Integer Implements IComparer.Compare
            Dim fileInfo1 As FileInfo = info1
            Dim fileInfo2 As FileInfo = info2
            Dim fileSize1 As Long
            If fileInfo1 Is Nothing Then
                Return -1
            Else : fileSize1 = fileInfo1.Length()
            End If
            Dim fileSize2 As Long
            If fileInfo2 Is Nothing Then
                Return -1
            Else : fileSize2 = fileInfo2.Length()
            End If
            If fileSize1 > fileSize2 Then
                Return 1
            End If
            If fileSize1 < fileSize2 Then
                Return -1
            End If
            Return 0
        End Function 'Compare
    End Class 'SizeComparer

    Shared Sub Main()
        Dim myTest As New Test()
        ' Specify the directory whose contents you want to list and sort.
        myTest.ProcessDirectory(New DirectoryInfo("c:\Drivers\net\removed"))
    End Sub 'Main'

End Class 'Test'

[C#] 
using System;
using System.IO;
using System.Collections;

class Test
{
    IComparer sizeComparer = new SizeComparer();
    private void ProcessDirectory(DirectoryInfo dir)
    {
        FileSystemInfo[] infos = dir.GetFileSystemInfos();
        Array.Sort(infos, sizeComparer);
        foreach (FileSystemInfo f in infos)
        {
            if (f is DirectoryInfo) 
            {
                ProcessDirectory((DirectoryInfo)f);                             
            }
            else if (f is FileInfo) 
            {
                ProcessFile((FileInfo)f);
            }
        }
    }

    private void ProcessFile(FileInfo f) 
    {
        Console.WriteLine("{0,16:N0} {1}", f.Length, f.FullName);
    }

    class SizeComparer: IComparer
    {
        public int Compare(object info1, object info2) 
        {
            FileInfo fileInfo1 = info1 as FileInfo;
            FileInfo fileInfo2 = info2 as FileInfo;
            long fileSize1 = fileInfo1 == null? -1: fileInfo1.Length;
            long fileSize2 = fileInfo2 == null? -1: fileInfo2.Length;
            if (fileSize1 > fileSize2) return 1;
            if (fileSize1 < fileSize2) return -1;
            return 0;
        }
    }
    static void Main() 
    {
        // Specify the directory whose contents you want to list and sort.
        new Test().ProcessDirectory(new DirectoryInfo(@"C:\Drivers\net\removed"));
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;
using namespace System::Collections;

__gc class Test {
private:
    __gc class SizeComparer : public IComparer {
    public:
        int Compare(Object* info1, Object* info2) {
            FileInfo* fileInfo1 = dynamic_cast<FileInfo*>(info1);
            FileInfo* fileInfo2 = dynamic_cast<FileInfo*>(info2);
            __int64 fileSize1 = fileInfo1 ? fileInfo1->Length : -1;
            __int64 fileSize2 = fileInfo2 ? fileInfo2->Length : -1;
            if (fileSize1 > fileSize2) return 1;
            if (fileSize1 < fileSize2) return -1;
            return 0;
        }
    };

    IComparer* sizeComparer;

    void ProcessFile(FileInfo* f) {
        Console::WriteLine("{0,16:N0} {1}", f->Length.ToString(), f->FullName);
    }

public:
    Test() {
        sizeComparer = new SizeComparer();
    }

    void ProcessDirectory(DirectoryInfo* dir) {
        FileSystemInfo* infos __gc[] = dir->GetFileSystemInfos();
        Array::Sort(infos, sizeComparer);

        IEnumerator* en = infos->GetEnumerator();
        FileSystemInfo* f;
        while (en->MoveNext()) {
            f = __try_cast<FileSystemInfo*>(en->Current);
            if (dynamic_cast<DirectoryInfo*>(f) != 0) {
                ProcessDirectory(dynamic_cast<DirectoryInfo*>(f));
            } else if (dynamic_cast<FileInfo*>(f) != 0) {
                ProcessFile(dynamic_cast<FileInfo*>(f));
            }
        }
    }

};

int main() {
    // Specify the directory whose contents you want to list and sort.
    (new Test())->ProcessDirectory(new DirectoryInfo(S"C:\\Drivers\\net\\removed"));
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

DirectoryInfo Class | DirectoryInfo Members | System.IO Namespace | DirectoryInfo.GetFileSystemInfos Overload List | FileSystemInfo | Working with I/O | Reading Text from a File | Writing Text to a File

Show:
© 2014 Microsoft