How to read files in a folder or Directory in C#?

This code snippet reads a directory and lists all the files in this directory including the file size and creation date.

using System;
using System.IO;

namespace FileOperationsSample
    class Program
        static void Main(string[] args)
            // Change this path to the directory you want to read
            string path = "C:\\Junk";            
            DirectoryInfo dir = new DirectoryInfo(path);
            Console.WriteLine("File Name                       Size        Creation Date and Time");
            foreach (FileInfo flInfo in dir.GetFiles())
                String name = flInfo.Name;
                long size = flInfo.Length;
                DateTime creationTime = flInfo.CreationTime;
                Console.WriteLine("{0, -30:g} {1,-12:N0} {2} ", name, size, creationTime);


Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now