Get List Of Physical & Logical Drives
-
I'm trying to load a list of drives. If you went to Windows Explorer and clicked on This PC, you would see all of the drives and devices available to you, physically installed, USB, and phones/tablets. This is the list I want In addition to my C: hard drive, I have a Lenovo tablet, my Samsung S22 Ultra phone, a USB DVD drive, Seagate USB HDD all plugged in. So, first I use this to get the HD and DVD drives:
var allDrives = DriveInfo.GetDrives();
First Problem This gives me the drives, but for the DVD drives the DriveType is showing as 'CDRom'. Second Problem I want to get tablets and phones that are plugged in. I found this[^], which works well, with one small problem. Based on that I wrote this bit of code that writes out some of the drive meta data: I get the ClassId from here[^]. Maybe my class Id is wrong??
public static List GetUSBDevices()
{
var file = @"c:\projects\usbinfo.csv";
if (File.Exists(file))
{
File.Delete(file);
}var devices = new List(); ManagementObjectCollection collection; using (var searcher = new ManagementObjectSearcher("Select \* From Win32\_PnPEntity")) { collection = searcher.Get(); } using (var sw = new StreamWriter(file, true)) { var line = ""; var keys = new string\[\] { "Name", "Description", "Caption", "ClassGuid", }; // Write out header line foreach (var key in keys) { line += $"{key},"; } sw.WriteLine(line); line = ""; foreach (var device in collection) { var name = (string)device.GetPropertyValue("Name"); var classGuid = (string)device.GetPropertyValue("ClassGuid"); if (classGuid == "{eec5ad98-8080-425f-922a-dabf3de3f69a}") { foreach (var key in keys)
-
I'm trying to load a list of drives. If you went to Windows Explorer and clicked on This PC, you would see all of the drives and devices available to you, physically installed, USB, and phones/tablets. This is the list I want In addition to my C: hard drive, I have a Lenovo tablet, my Samsung S22 Ultra phone, a USB DVD drive, Seagate USB HDD all plugged in. So, first I use this to get the HD and DVD drives:
var allDrives = DriveInfo.GetDrives();
First Problem This gives me the drives, but for the DVD drives the DriveType is showing as 'CDRom'. Second Problem I want to get tablets and phones that are plugged in. I found this[^], which works well, with one small problem. Based on that I wrote this bit of code that writes out some of the drive meta data: I get the ClassId from here[^]. Maybe my class Id is wrong??
public static List GetUSBDevices()
{
var file = @"c:\projects\usbinfo.csv";
if (File.Exists(file))
{
File.Delete(file);
}var devices = new List(); ManagementObjectCollection collection; using (var searcher = new ManagementObjectSearcher("Select \* From Win32\_PnPEntity")) { collection = searcher.Get(); } using (var sw = new StreamWriter(file, true)) { var line = ""; var keys = new string\[\] { "Name", "Description", "Caption", "ClassGuid", }; // Write out header line foreach (var key in keys) { line += $"{key},"; } sw.WriteLine(line); line = ""; foreach (var device in collection) { var name = (string)device.GetPropertyValue("Name"); var classGuid = (string)device.GetPropertyValue("ClassGuid"); if (classGuid == "{eec5ad98-8080-425f-922a-dabf3de3f69a}") { foreach (var key in keys)
Kevin Marois wrote:
With GetDriveInfo, how do I know if a drive that says CDRom is really a DVDRom?
You can't.
GetDriveInfo
relies on a Win32 API, and theDriveType
enum is mapped to the Win32 drive types, which haven't been updated to include DVD/Blu-Ray types.GetDriveTypeA function (fileapi.h) - Win32 apps | Microsoft Learn[^]:
DRIVE_UNKNOWN The drive type cannot be determined. DRIVE_NO_ROOT_DIR The root path is invalid; for example, there is no volume mounted at the specified path. DRIVE_REMOVABLE The drive has removable media; for example, a floppy drive, thumb drive, or flash card reader. DRIVE_FIXED The drive has fixed media; for example, a hard disk drive or flash drive. DRIVE_REMOTE The drive is a remote (network) drive. DRIVE_CDROM The drive is a CD-ROM drive. DRIVE_RAMDISK The drive is a RAM disk.
There is an extended list of disk types defined in the IMAPI2 library: IMAPI_MEDIA_PHYSICAL_TYPE (imapi2.h) - Win32 apps | Microsoft Learn[^] You may be able to reference and use that library, as described in this SO answer[^].
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Kevin Marois wrote:
With GetDriveInfo, how do I know if a drive that says CDRom is really a DVDRom?
You can't.
GetDriveInfo
relies on a Win32 API, and theDriveType
enum is mapped to the Win32 drive types, which haven't been updated to include DVD/Blu-Ray types.GetDriveTypeA function (fileapi.h) - Win32 apps | Microsoft Learn[^]:
DRIVE_UNKNOWN The drive type cannot be determined. DRIVE_NO_ROOT_DIR The root path is invalid; for example, there is no volume mounted at the specified path. DRIVE_REMOVABLE The drive has removable media; for example, a floppy drive, thumb drive, or flash card reader. DRIVE_FIXED The drive has fixed media; for example, a hard disk drive or flash drive. DRIVE_REMOTE The drive is a remote (network) drive. DRIVE_CDROM The drive is a CD-ROM drive. DRIVE_RAMDISK The drive is a RAM disk.
There is an extended list of disk types defined in the IMAPI2 library: IMAPI_MEDIA_PHYSICAL_TYPE (imapi2.h) - Win32 apps | Microsoft Learn[^] You may be able to reference and use that library, as described in this SO answer[^].
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Great thanks. I'll take a look
In theory, theory and practice are the same. But in practice, they never are.” If it's not broken, fix it until it is. Everything makes sense in someone's mind.