Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all drives in PC with .NET using C#

Tags:

c#

.net

how to get all drives in a PC. and types of every drive and free-space of each

i.e. System-Drive, CD-Drive, DVD-Drive, Removable, ... etc.

If a system is attached with a new drive may be a pen drive or external hard disc.

How to detect them at time of attachment ?

like image 671
Javed Akram Avatar asked Mar 04 '11 15:03

Javed Akram


2 Answers

To get a list of the drives, you can use the System.IO.DriveInfo class:

foreach(var drive in DriveInfo.GetDrives())
{
    Console.WriteLine("Drive Type: {0}", drive.DriveType);
    Console.WriteLine("Drive Size: {0}", drive.TotalSize);
    Console.WriteLine("Drive Free Space: {0}", drive.TotalFreeSpace);
}

Unfortunately, this doesn't provide a way to listen for USB Key Insertions. There is another question dealing with that you could check out:

.NET - Detecting USB Drive Insertion and Removal...

like image 165
Justin Niessner Avatar answered Nov 03 '22 04:11

Justin Niessner


        public string getalldrivestotalnfreespace()
    {
        string s = "    Drive          Free Space   TotalSpace     FileSystem    %Free Space       DriveType\n\r========================================================================================\n\r";
        foreach (DriveInfo drive in DriveInfo.GetDrives())
        {
            double ts = 0;
            double fs = 0;
            double frprcntg = 0;
            long divts = 1024 * 1024 * 1024;
            long divfs = 1024 * 1024 * 1024;
            string tsunit = "GB";
            string fsunit = "GB";
            if (drive.IsReady)
            {
                fs = drive.TotalFreeSpace;
                ts = drive.TotalSize;
                frprcntg = (fs / ts) * 100;
                if (drive.TotalSize < 1024)
                {
                    divts =1; tsunit = "Byte(s)";
                }
                else if (drive.TotalSize < (1024*1024))
                {
                    divts = 1024; tsunit = "KB";
                }
                else if (drive.TotalSize < (1024 * 1024*1024))
                {
                    divts = 1024*1024; tsunit = "MB";
                }
                //----------------------
                if (drive.TotalFreeSpace < 1024)
                {
                    divfs = 1; fsunit = "Byte(s)";
                }
                else if (drive.TotalFreeSpace < (1024 * 1024))
                {
                    divfs = 1024; fsunit = "KB";
                }
                else if (drive.TotalFreeSpace < (1024 * 1024 * 1024))
                {
                    divfs = 1024 * 1024; fsunit = "MB";
                }
                s = s +
                " " + drive.VolumeLabel.ToString() +
                "[" + drive.Name.Substring(0, 2) +
                "]\t" + String.Format("{0,10:0.0}", ((fs / divfs)).ToString("N2")) + fsunit +
                String.Format("{0,10:0.0}", (ts / divts).ToString("N2")) + tsunit +
                "\t" + drive.DriveFormat.ToString() + "\t\t" + frprcntg.ToString("N2") + "%"+
                "\t\t" + drive.DriveType.ToString();

                s = s + "\n\r";
            }
        }
        return s;
    }

The ouput should look like :- enter image description here

like image 39
MD. Mohiuddin Ahmed Avatar answered Nov 03 '22 03:11

MD. Mohiuddin Ahmed