How to get Information about Files, Folders, and Drive in C#

Hi Programmers, welcome to new article of c#.net. This article i’ll write program to display information about files, folders, and drives using c# console application. For getting files, folders, and drives information we should use FileInfo, DirectoryInfo and DriveInfo class. Create the object of class and call following properties. Let’s write the codes.

Directly use below codes in Editor.

using System;
using System.IO;
namespace ConsoleApp9 
{
    class Program
    {
        static void Main(string[] args)
        {
            DriveInfo di = new DriveInfo(@"C:\Users\Adam\Desktop");
            Console.WriteLine("Drive Name :" + di.Name);
            Console.WriteLine("Driver Information : ");
            Console.WriteLine("Total Size :" + di.TotalSize);
            Console.WriteLine("Total Free Space :" +di.TotalFreeSpace);
            Console.WriteLine("Volume Label :" + di.VolumeLabel);
            Console.WriteLine("Drive Type :" + di.DriveType);

            Console.WriteLine("Directory Information : ");
            DirectoryInfo dir = di.RootDirectory;
            Console.WriteLine(dir.Attributes.ToString());
            DirectoryInfo[] dirInf = dir.GetDirectories("*.*");
            foreach (DirectoryInfo d in dirInf)
            {
                Console.WriteLine(d.Name);
            }
            Console.WriteLine("File  Information : ");
            FileInfo[] fNames = dir.GetFiles("*.*");
            foreach (FileInfo fi in fNames)
            {
               Console.WriteLine("{0}: {1}: {2}", fi.Name, fi.LastAccessTime, fi.Length);
            }       
           }
        }
    }

Happy Coding…Thanks.

Post Author: adama