DirectoryInfo Example

In the spirit of our DriveInfo example, which was a little slip of a thing, here’s a DriveInfo example.

<pre>
using System;
using System.Collections.Generic;
using System.IO;

class DirectoryInfoExample
{
    static void Main(string[] args)
    {
        string dir = @"C:\";
        Console.WriteLine("Listing directories underneath {0}", dir);
        DirectoryInfo diList = new DirectoryInfo(dir);
        foreach (DirectoryInfo di in diList.GetDirectories())
        {
            Console.WriteLine("{0}", di.FullName);
        }
        Console.WriteLine("Listing files in {0}", dir);

        foreach (FileInfo fi in diList.GetFiles())
        {
            Console.WriteLine("File name:  {0}\nCreated:  {1},\nLength:   {2} bytes",
                 fi.Name, fi.CreationTime, fi.Length);
        }
        Console.ReadKey();    
    }
}
</pre>

With over twenty years of software development experience, including building a successful career as a self-taught developer, John Lockwood creates career-focused training videos to help others launch or improve their software engineering careers.

Posted in Miscellaneous

Java Courses

Sign up for news and exclusive offers on new courses.