9

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 ?

Ian Ringrose
  • 51,220
  • 55
  • 213
  • 317
Javed Akram
  • 15,024
  • 26
  • 81
  • 118

9 Answers9

14

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...

Community
  • 1
  • 1
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
3
        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

MD. Mohiuddin Ahmed
  • 2,092
  • 2
  • 26
  • 35
  • 1
    Just mentioning: you can actually include a picture in your post by just uploading it (click on the `insert picture` icon. This way it can appear inline instead of requiring to follow your link. – Qortex Mar 08 '13 at 21:46
2
Environment.GetLogicalDrives();

MSDN link

PVitt
  • 11,500
  • 5
  • 51
  • 85
2

You can get the drives and info quite easily

DriveInfo[] drives = DriveInfo.GetDrives();

foreach (DriveInfo drive in drives) 
{
    Console.WriteLine(drive.Name);
    Console.WriteLine(drive.TotalSize);
}

There's a good article on detecting adding/removing drives on CodeProject

Peter Kelly
  • 14,253
  • 6
  • 54
  • 63
1
 Environment.GetLogicalDrives();

MSDN link

Bala R
  • 107,317
  • 23
  • 199
  • 210
1

The WMI libraries will probably help. Also there's a codeproject article that talks about this:

http://www.codeproject.com/KB/cs/UsbManager.aspx

Phil Gan
  • 2,813
  • 2
  • 29
  • 38
0

Here is the answer to your first question. To get all logical drives you can try this:

string[] drives = Environment.GetLogicalDrives();
Dan Waterbly
  • 850
  • 7
  • 15
0

The System.IO.DriveInfo class is the place to start. The DriveType property can tell you what you are looking for.

vcsjones
  • 138,677
  • 31
  • 291
  • 286
0

To get all the drives currently attached:

DriveInfo[] allDrives = DriveInfo.GetDrives();

And for the detection you have to listen to WMI events. Check this out: http://www.techtalkz.com/c-c-sharp/182048-need-detect-insertion-removal-usb-drive.html

Mikael Östberg
  • 16,982
  • 6
  • 61
  • 79