How to: Create a Directory Listing

The following code example shows how to use the I/O classes to create a listing of all files with the extension ".exe" in a directory.

Example

Imports System
Imports System.IO

Public Class DirectoryLister
    Public Shared Sub Main(args() As String)
        Dim path As String = Environment.CurrentDirectory
        If args.Length > 0 Then
            If Directory.Exists(args(0)) Then
                path = args(0)
            Else
                Console.WriteLine("{0} not found; using current directory:",
                    args(0))
            End If
        End If
        Dim dir As New DirectoryInfo(path)
        For Each f As FileInfo In dir.GetFiles("*.exe")
            Dim name As String = f.Name
            Dim size As Long = f.Length
            Dim creationTime As DateTime = f.CreationTime
            Console.WriteLine("{0,-12:N0} {1,-20:g} {2}", size,
                creationTime, name)
        Next f
    End Sub
End Class
using System;
using System.IO;

public class DirectoryLister
{
    public static void Main(String[] args)
    {
        string path = Environment.CurrentDirectory;
        if (args.Length > 0)
        {
            if (Directory.Exists(args[0]))
            {
                path = args[0];
            }
            else
            {
                Console.WriteLine("{0} not found; using current directory:",
                    args[0]);
            }
        }
        DirectoryInfo dir = new DirectoryInfo(path);
        foreach (FileInfo f in dir.GetFiles("*.exe"))
        {
            string name = f.Name;
            long size = f.Length;
            DateTime creationTime = f.CreationTime;
            Console.WriteLine("{0,-12:N0} {1,-20:g} {2}", size,
                creationTime, name);
        }
    }
}
using namespace System;
using namespace System::IO;

public ref class DirectoryLister
{
public:
    static void Main(array<String^>^ args)
    {
        String^ path = Environment::CurrentDirectory;
        if (args->Length > 0)
        {
            if (Directory::Exists(args[0]))
            {
                path = args[0];
            }
            else
            {
                Console::WriteLine("{0} not found; using current directory:",
                    args[0]);
            }
        }
        DirectoryInfo^ dir = gcnew DirectoryInfo(path);
        for each (FileInfo^ f in dir->GetFiles("*.exe"))
        {
            String^ name = f->Name;
            long size = f->Length;
            DateTime^ creationTime = f->CreationTime;
            Console::WriteLine("{0,-12:N0} {1,-20:g} {2}", size,
                creationTime, name);
        }
    }
};

int main()
{
    DirectoryLister::Main(Environment::GetCommandLineArgs());
}

Robust Programming

In this example, the DirectoryInfo is the current directory, denoted by ("."), and the code lists all files in the current directory having a .exe extension, along with their file size, creation time, and name. Assuming that there were .exe files in the \Bin subdirectory of C:\MyDir, the output of this code might look like this:

953          7/20/2000 10:42 AM   C:\MyDir\Bin\paramatt.exe
664          7/27/2000 3:11 PM    C:\MyDir\Bin\tst.exe
403          8/8/2000 10:25 AM    C:\MyDir\Bin\dirlist.exe

If you want a list of files in another directory, such as your C:\ root directory, pass the argument "C:\" into the executable generated by compiling this code, for example: "testApplication.exe C:\".

Note

Visual Basic users may choose to use the methods and properties provided by the FileSystem class for file I/O.

See Also

Tasks

How to: Read and Write to a Newly Created Data File

How to: Open and Append to a Log File

How to: Read Text from a File

How to: Write Text to a File

How to: Read Characters from a String

How to: Write Characters to a String

Reference

DirectoryInfo

CreationTime

FullName

FileInfo.Length

DirectoryInfo.GetFiles

Concepts

Basic File I/O