WMI Hard Drive physical location and drive letter problem

  • Thread starter Thread starter jimdscudder
  • Start date Start date
J

jimdscudder

How can I use WMI or a WqlObjectQuery to find the hard drive letter of the
physical drive location index.

For example the following code will give me the physical drive location:
StringCollection propNames = new StringCollection();
ManagementClass driveClass = new ManagementClass("Win32_DiskDrive");
PropertyDataCollection props = driveClass.Properties;
foreach (PropertyData driveProperty in props)
{
propNames.Add(driveProperty.Name);
}
ManagementObjectCollection drives = driveClass.GetInstances();
foreach (ManagementObject drv in drives)
{
string id = "Index";
this.listBox1.Items.Add(" ******** Drive( " + drv[id].ToString() + " )
Location ************");
}

And, the follwoing code will give me the drive letter:

SelectQuery query = new SelectQuery( "select name, FreeSpace, SystemName
from win32_logicaldisk where drivetype=3" );
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
foreach (ManagementObject mo in searcher.Get())
{
this.listBox1.Items.Add( "Drive letter is: " + mo["name"] );
}

How can I do a query using the drive location and get the drive letter
assigned to it?
 
How can I use WMI or a WqlObjectQuery to find the hard drive letter of the
physical drive location index.

Can't you just get all Win32_DiskDrivePhysicalMedia instances? It does
that mapping for you.



Mattias
 
jimdscudder said:
How can I use WMI or a WqlObjectQuery to find the hard drive letter of
the
physical drive location index.

For example the following code will give me the physical drive location:
StringCollection propNames = new StringCollection();
ManagementClass driveClass = new ManagementClass("Win32_DiskDrive");
PropertyDataCollection props = driveClass.Properties;
foreach (PropertyData driveProperty in props)
{
propNames.Add(driveProperty.Name);
}
ManagementObjectCollection drives = driveClass.GetInstances();
foreach (ManagementObject drv in drives)
{
string id = "Index";
this.listBox1.Items.Add(" ******** Drive( " + drv[id].ToString() + " )
Location ************");
}

And, the follwoing code will give me the drive letter:

SelectQuery query = new SelectQuery( "select name, FreeSpace, SystemName
from win32_logicaldisk where drivetype=3" );
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
foreach (ManagementObject mo in searcher.Get())
{
this.listBox1.Items.Add( "Drive letter is: " + mo["name"] );
}

How can I do a query using the drive location and get the drive letter
assigned to it?

It's not really a good idea to do this starting from the Win32_Diskdrive as
you will incur some overhead when querying for the associated
Win32_LogicalDisk class as this requires a floppy disk access.
Anyway, what you could do is query for the associated classes and as such go
down until you have found the Win32_LogicalDisk class(es), note that a
physical disk can have multiple partitions and so multiple logical disks. I
also suggest you consult the WMI sdk docs to get a better understanding of
the CIM class hierarchy

using(devs = new ManagementClass( @"Win32_Diskdrive"))
{
ManagementObjectCollection moc = devs.GetInstances();
foreach(ManagementObject mo in moc)
{
Console.WriteLine(mo["DeviceId"]);
foreach (ManagementObject b in mo.GetRelated("Win32_DiskPartition"))
{
Console.WriteLine("{0}", b["Name"]);
foreach (ManagementBaseObject c in
b.GetRelated("Win32_LogicalDisk"))
Console.WriteLine("{0}", c["Name"]);
}
}
}

Willy.
 
Not really, Win32_DiskDrivePhysicalMedia does the mapping PhysicalMedia -
DiskDrive , not DiskDrive - LogicalDisk.

Willy.
 
With the Win32_DiskDrivePhysicalMedia query you get:
\\UserName\root\cimv2:Win32_PhysicalMedia.Tag="\\\\.\\PHYSICALDRIVE0"
\\UserName\root\cimv2:Win32_DiskDrive.DeviceID="\\\\.\\PHYSICALDRIVE0"
\\UserName\root\cimv2:Win32_PhysicalMedia.Tag="\\\\.\\PHYSICALDRIVE1"
\\UserName\root\cimv2:Win32_DiskDrive.DeviceID="\\\\.\\PHYSICALDRIVE1"

Thanks Jim
Willy Denoyette said:
Not really, Win32_DiskDrivePhysicalMedia does the mapping PhysicalMedia -
DiskDrive , not DiskDrive - LogicalDisk.

Willy.
 
Hurray! Hurray! A thousands blessings on you!!

output:
\\.\PHYSICALDRIVE0
C:
\\.\PHYSICALDRIVE1
E:

The code I used(thanks to you!):

private void Drive_Letter_PhisicalAddress_Letter()
{
StreamWriter sw = new
StreamWriter(@"c:\test\DiskDrive_indx_letter.txt");
//using(devs = new ManagementClass( @"Win32_Diskdrive"))
ManagementClass devs = new ManagementClass( @"Win32_Diskdrive");
{
ManagementObjectCollection moc = devs.GetInstances();
foreach(ManagementObject mo in moc)
{
//Console.WriteLine(mo["DeviceId"]);
this.listBox1.Items.Add(mo["DeviceId"]);
sw.WriteLine(mo["DeviceId"]);
foreach (ManagementObject b in
mo.GetRelated("Win32_DiskPartition"))
{
Console.WriteLine("{0}", b["Name"]);
foreach (ManagementBaseObject c in
b.GetRelated("Win32_LogicalDisk"))
{
//Console.WriteLine("{0}", c["Name"]);
this.listBox1.Items.Add(c["Name"]);
sw.WriteLine(c["Name"]);
}
}
}
}
sw.Close();
}




Willy Denoyette said:
jimdscudder said:
How can I use WMI or a WqlObjectQuery to find the hard drive letter of
the
physical drive location index.

For example the following code will give me the physical drive location:
StringCollection propNames = new StringCollection();
ManagementClass driveClass = new ManagementClass("Win32_DiskDrive");
PropertyDataCollection props = driveClass.Properties;
foreach (PropertyData driveProperty in props)
{
propNames.Add(driveProperty.Name);
}
ManagementObjectCollection drives = driveClass.GetInstances();
foreach (ManagementObject drv in drives)
{
string id = "Index";
this.listBox1.Items.Add(" ******** Drive( " + drv[id].ToString() + " )
Location ************");
}

And, the follwoing code will give me the drive letter:

SelectQuery query = new SelectQuery( "select name, FreeSpace, SystemName
from win32_logicaldisk where drivetype=3" );
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
foreach (ManagementObject mo in searcher.Get())
{
this.listBox1.Items.Add( "Drive letter is: " + mo["name"] );
}

How can I do a query using the drive location and get the drive letter
assigned to it?

It's not really a good idea to do this starting from the Win32_Diskdrive
as you will incur some overhead when querying for the associated
Win32_LogicalDisk class as this requires a floppy disk access.
Anyway, what you could do is query for the associated classes and as such
go down until you have found the Win32_LogicalDisk class(es), note that a
physical disk can have multiple partitions and so multiple logical disks.
I also suggest you consult the WMI sdk docs to get a better understanding
of the CIM class hierarchy

using(devs = new ManagementClass( @"Win32_Diskdrive"))
{
ManagementObjectCollection moc = devs.GetInstances();
foreach(ManagementObject mo in moc)
{
Console.WriteLine(mo["DeviceId"]);
foreach (ManagementObject b in
mo.GetRelated("Win32_DiskPartition"))
{
Console.WriteLine("{0}", b["Name"]);
foreach (ManagementBaseObject c in
b.GetRelated("Win32_LogicalDisk"))
Console.WriteLine("{0}", c["Name"]);
}
}
}

Willy.
 
Not really, Win32_DiskDrivePhysicalMedia does the mapping PhysicalMedia -
DiskDrive , not DiskDrive - LogicalDisk.

Oops, you're right of course. Thanks for catching that.



Mattias
 
Hello,

I am hoping I missed something obvious here, but when I tried this example,
it accessed the floppy drive each time it enumerated a physical device.



Willy Denoyette said:
jimdscudder said:
How can I use WMI or a WqlObjectQuery to find the hard drive letter of
the
physical drive location index.

For example the following code will give me the physical drive location:
StringCollection propNames = new StringCollection();
ManagementClass driveClass = new ManagementClass("Win32_DiskDrive");
PropertyDataCollection props = driveClass.Properties;
foreach (PropertyData driveProperty in props)
{
propNames.Add(driveProperty.Name);
}
ManagementObjectCollection drives = driveClass.GetInstances();
foreach (ManagementObject drv in drives)
{
string id = "Index";
this.listBox1.Items.Add(" ******** Drive( " + drv[id].ToString() + " )
Location ************");
}

And, the follwoing code will give me the drive letter:

SelectQuery query = new SelectQuery( "select name, FreeSpace, SystemName
from win32_logicaldisk where drivetype=3" );
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
foreach (ManagementObject mo in searcher.Get())
{
this.listBox1.Items.Add( "Drive letter is: " + mo["name"] );
}

How can I do a query using the drive location and get the drive letter
assigned to it?

It's not really a good idea to do this starting from the Win32_Diskdrive
as you will incur some overhead when querying for the associated
Win32_LogicalDisk class as this requires a floppy disk access.
Anyway, what you could do is query for the associated classes and as such
go down until you have found the Win32_LogicalDisk class(es), note that a
physical disk can have multiple partitions and so multiple logical disks.
I also suggest you consult the WMI sdk docs to get a better understanding
of the CIM class hierarchy

using(devs = new ManagementClass( @"Win32_Diskdrive"))
{
ManagementObjectCollection moc = devs.GetInstances();
foreach(ManagementObject mo in moc)
{
Console.WriteLine(mo["DeviceId"]);
foreach (ManagementObject b in
mo.GetRelated("Win32_DiskPartition"))
{
Console.WriteLine("{0}", b["Name"]);
foreach (ManagementBaseObject c in
b.GetRelated("Win32_LogicalDisk"))
Console.WriteLine("{0}", c["Name"]);
}
}
}

Willy.
 
Back
Top