Click here to Skip to main content
15,918,889 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
My computer has 5 drives they are
C D E F G
the hard disk drive has 2 partition
How can i know that drive C or D or E or F or G
is belong to the hard disk, one or more could belong to usb(flash)
my goal is to write data to the hard disk i mean to the 2nd partion
of the hard disk which is more likley D
but D is not alaways 2nd partion of the hard disk
C#
int i;
string[] strArr = new string[16];
DriveInfo[] allDrives = DriveInfo.GetDrives();
i = 0;
foreach (DriveInfo d in allDrives)
{
    strArr[i] = d.Name;
    i++;
    // is d.Name hard disk or usb or cd etc
}
Posted
Updated 7-Sep-13 4:45am
v4

Many, if not all, new PCs have Recovery Partitions that are the second partition of a hard drive. Most vendors would advise that it is not in your best interest to alter data in the Recover Partition.

You can use System.Io.DriveType enumeration to differentiate drive type.
The example below selects the optical drive (CD or DVD).

C#
string[] Drives = System.IO.Directory.GetLogicalDrives();
foreach (string strDrive in Drives) {
    System.IO.DriveInfo di = new System.IO.DriveInfo(strDrive.Substring(0, 1).ToUpper());
    // Optical Drive (CD or DVD)
    if (di.DriveType == System.IO.DriveType.CDRom) {
        strDVD = strDrive.Substring(0, 2);
        // example: F:
    }
}


A hard disk would be found using:
if (di.DriveType == System.IO.DriveType.Fixed) {


System.IO.DriveType Enumeration
Quote:
Unknown The type of drive is unknown.
NoRootDirectory The drive does not have a root directory.
Removable The drive is a removable storage device, such as a floppy disk drive or a USB flash drive.
Fixed The drive is a fixed disk.
Network The drive is a network drive.
CDRom The drive is an optical disc device, such as a CD or DVD-ROM.
Ram The drive is a RAM disk.
 
Share this answer
 
v5
int i;
string[] strArr = new string[16];
DriveInfo[] allDrives = DriveInfo.GetDrives();
i = 0;
foreach (DriveInfo d in allDrives)
{
    strArr[i] = d.Name;
    i++;
    MessageBox.Show("Type of Drive is "+d.Drive.Type.ToString());
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900