GetFiles Method

Directory.GetFiles Method

Returns the names of files in the specified directory.

Overload List

Returns the names of files in the specified directory.

Supported by the .NET Compact Framework.

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

Returns the names of files in the specified directory that match the specified search pattern.

Supported by the .NET Compact Framework.

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

Example

[Visual Basic, C#, C++] The following example counts the number of files that begin with 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
            ' Only get files that begin with the letter "c."
            Dim dirs As String() = Directory.GetFiles("c:\", "c*")
            Console.WriteLine("The number of files starting with c is {0}.", dirs.Length)
            Dim dir As String
            For Each dir In dirs
                Console.WriteLine(dir)
            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 
        {
            // Only get files that begin with the letter "c."
            string[] dirs = Directory.GetFiles(@"c:\", "c*");
            Console.WriteLine("The number of files starting with c is {0}.", dirs.Length);
            foreach (string dir in dirs) 
            {
                Console.WriteLine(dir);
            }
        } 
        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 {
        // Only get files that begin with the letter "c."
        String* dirs[] = Directory::GetFiles(S"c:\\", S"c*");
        Console::WriteLine(S"The number of files starting with c is {0}.", __box(dirs->Length));
        Collections::IEnumerator* myEnum = dirs->GetEnumerator();
        while (myEnum->MoveNext()) {
            Console::WriteLine(myEnum->Current);
        }
    } 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

Directory Class | Directory Members | System.IO Namespace

Show:
© 2016 Microsoft