Export (0) Print
Expand All

DirectoryInfo.GetFiles Method

Returns a file list from the current directory.

Overload List

Returns a file list from the current directory.

Supported by the .NET Compact Framework.

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

Returns a file list from the current directory matching the given searchPattern, such as "*.txt".

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function GetFiles(String) As FileInfo()
[C#] public FileInfo[] GetFiles(string);
[C++] public: FileInfo* GetFiles(String*) [];
[JScript] public function GetFiles(String) : FileInfo[];

Example

[Visual Basic, C#, C++] The following example counts the directories and files that contain the specified letter.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of GetFiles. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Imports System
Imports System.IO

Public Class Test
    Public Shared Sub Main()
        Try
            Dim di As DirectoryInfo = New DirectoryInfo("c:\")
            'Get only subdirectories that contain the letter "p."
            Dim dirs As DirectoryInfo() = di.GetDirectories("*p*")
            Console.WriteLine("Number of directories with a p: {0}", dirs.Length)
            Dim diNext As DirectoryInfo
            ' Count all files in each subdirectory that contain the letter "e."
            For Each diNext In dirs
                Console.WriteLine("The number of files in {0} with an e is {1}", _
                 diNext, diNext.GetFiles("*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."
            DirectoryInfo[] 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 in {0} with an e is {1}", diNext, 
                    diNext.GetFiles("*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 "p."
        DirectoryInfo* 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 S"e."
        Collections::IEnumerator* myEnum = dirs->GetEnumerator();
        while (myEnum->MoveNext()) {
            DirectoryInfo* diNext = __try_cast<DirectoryInfo*>(myEnum->Current);
            Console::WriteLine(S"The number of files in {0} with an e is {1}", diNext, 
                __box(diNext->GetFiles(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.

See Also

DirectoryInfo Class | DirectoryInfo Members | System.IO Namespace

Show:
© 2014 Microsoft