Windows apps
Collapse the table of content
Expand the table of content
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Creating a Directory Listing

The following code example shows how to use the I/O classes to create a listing of a directory.

Option Explicit On 
Option Strict On
Imports System
Imports System.IO
Class DirectoryLister
    Public Shared Sub Main()
        Dim dir As New DirectoryInfo(".")
        Dim f As FileInfo
        For Each f In dir.GetFiles("*.cs")
            Dim name As [String] = f.FullName
            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;
class DirectoryLister
    public static void Main(String[] args)
        DirectoryInfo dir = new DirectoryInfo(".");
        foreach (FileInfo f in dir.GetFiles("*.cs")) 
            String name = f.FullName;
            long size = f.Length;
            DateTime creationTime = f.CreationTime;
            Console.WriteLine("{0,-12:N0} {1,-20:g} {2}", size, 
                creationTime, name);

In this example, the DirectoryInfo is the current directory, denoted by ("."), and the code lists all files in the current directory having a .cs extension, along with their file size, creation time, and name. Assuming that there were .cs 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.cs
664          7/27/2000 3:11 PM    C:\MyDir\Bin\tst.cs
403          8/8/2000 10:25 AM    C:\MyDir\Bin\dirlist.cs

If you want a list of files in another directory, such as your C:\ root directory, remember to use the backslash (\) escape character, as in "C:\\". You can also use Unix-style slashes, as in "C:/".

See Also

Reading and Writing to a Newly Created Data File | Opening and Appending to a Log File | Reading Text from a File | Writing Text to a File | Reading Characters from a String | Writing Characters to a String | Basic File I/O | DirectoryInfo | CreationTime | FullName | FileInfo.Length | DirectoryInfo.GetFiles

© 2018 Microsoft