Export (0) Print
Expand All

DirectoryInfo.GetFileSystemInfos Method (String)

.NET Framework 1.1

Retrieves an array of strongly typed FileSystemInfo objects matching the specified search criteria.

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

Parameters

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

Return Value

An array of strongly typed FileSystemInfo objects matching the search criteria.

Exceptions

Exception Type Condition
ArgumentNullException searchPattern is a null reference (Nothing in Visual Basic).
DirectoryNotFoundException The specified path is invalid, such as being on an unmapped drive.
SecurityException The caller does not have the required permission.

Remarks

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 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 that match the specified search pattern.

[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} with an e is {1}", diNext, _
                 diNext.GetFileSystemInfos("*e*").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} with an e is {1}", diNext, 
                    diNext.GetFileSystemInfos("*e*").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 S"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} with an e is {1}", diNext, 
                __box(diNext->GetFileSystemInfos(S"*e*")->Length));
        }
    } catch (Exception* e) {
        Console::WriteLine(S"The process failed: {0}", e);
    }
}

[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

.NET Framework Security: 

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:
© 2015 Microsoft