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

Mahesh Chand Jul 06, 2009
To read files in a folder or Directory in C#. we writes the code to reads a directory and lists all the files in this directory including the file size and creation date.
  • 0
  • 0
  • 39027
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");
            Console.WriteLine("=================================================================");
            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);
            }
            Console.ReadLine();
        }
    }
}

Mahesh Chand

Mahesh Chand is founder of C# Corner. C# Corner founded in 1999 is a FREE member contributions based open platform for developers to solve problems, learn new technology and hang out.  Mahesh has been awar... Read more

COMMENT USING

PREMIUM SPONSORS

Hire Mobile & Web Developer on demand. 100% satisfaction. Try for 1 week or Money Back. Local and remote developers available all over USA.

TOP RESOURCES

MOST LIKED RESOURCES