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
  • 39833
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

Founder C# Corner, a member-contributions based community founded in 1999 focuses on character, values, learning, sharing, networking and career growth. Mahesh has been awarded prestigious... 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