This documentation is archived and is not being maintained.

DirectoryInfo.GetFiles Method ()

.NET Framework 1.1

Returns a file list from the current directory.

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

Return Value

An array of type FileInfo.

Exceptions

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

Remarks

If there are no files 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 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

See all the files in all subdirectories of a directory. GetFileSystemInfos
Find the size of a directory. Directory
Determine if a file exists. Exists
Determine if a directory exists. Exists
Get file attributes. GetAttributes
Set file attributes. SetAttributes

Example

The following example retrieves files from a specified directory.

[Visual Basic] 
Imports System
Imports System.IO
Public Class GetFilesTest
    Public Shared Sub Main()
        ' Make a reference to a directory.
        Dim di As New DirectoryInfo("c:\")
        ' Get a reference to each file in that directory.
        Dim fiArr As FileInfo() = di.GetFiles()
        ' Display the names of the files.
        Dim fri As FileInfo
        For Each fri In fiArr
            Console.WriteLine(fri.Name)
        Next fri
    End Sub 'Main
End Class 'GetFilesTest

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

public class GetFilesTest 
{
    public static void Main() 
    {
        // Make a reference to a directory.
        DirectoryInfo di = new DirectoryInfo("c:\\");

        // Get a reference to each file in that directory.
        FileInfo[] fiArr = di.GetFiles();

        // Display the names of the files.
        foreach (FileInfo fri in fiArr)
            Console.WriteLine(fri.Name);
    }
}

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

using namespace System;
using namespace System::IO;

int main() {
    // Make a reference to a directory.
    DirectoryInfo* di = new DirectoryInfo(S"c:\\");

    // Get a reference to each file in that directory.
    FileInfo* fiArr[] = di->GetFiles();

    // Display the names of the files.
    Collections::IEnumerator* myEnum = fiArr->GetEnumerator();
    while (myEnum->MoveNext()) {
        FileInfo* fri = __try_cast<FileInfo*>(myEnum->Current);
        Console::WriteLine(fri->Name);
    }
}

[JScript] 
import System;
import System.IO;

public class GetFilesTest {
    public static function Main() {

        // Make a reference to a directory.
        var di : DirectoryInfo = new DirectoryInfo("c:\\");

        // Get a reference to each file in that directory.
        var fiArr : FileInfo[] = di.GetFiles();

        // Display the names of the files.
        for(var i : int in fiArr)
            Console.WriteLine(fiArr[i].Name);
    }
}
GetFilesTest.Main();

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.GetFiles Overload List | Working with I/O | Reading Text from a File | Writing Text to a File

Show: