Hurray! Hurray! A thousands blessings on you!!
output:
\\.\PHYSICALDRI VE0
C:
\\.\PHYSICALDRI VE1
E:
The code I used(thanks to you!):
private void Drive_Letter_Ph isicalAddress_L etter()
{
StreamWriter sw = new
StreamWriter(@" c:\test\DiskDri ve_indx_letter. txt");
//using(devs = new ManagementClass ( @"Win32_Diskdri ve"))
ManagementClass devs = new ManagementClass ( @"Win32_Diskdri ve");
{
ManagementObjec tCollection moc = devs.GetInstanc es();
foreach(Managem entObject mo in moc)
{
//Console.WriteLi ne(mo["DeviceId"]);
this.listBox1.I tems.Add(mo["DeviceId"]);
sw.WriteLine(mo["DeviceId"]);
foreach (ManagementObje ct b in
mo.GetRelated(" Win32_DiskParti tion"))
{
Console.WriteLi ne("{0}", b["Name"]);
foreach (ManagementBase Object c in
b.GetRelated("W in32_LogicalDis k"))
{
//Console.WriteLi ne("{0}", c["Name"]);
this.listBox1.I tems.Add(c["Name"]);
sw.WriteLine(c["Name"]);
}
}
}
}
sw.Close();
}
"Willy Denoyette [MVP]" <wi************ *@telenet.be> wrote in message
news:uw******** ******@TK2MSFTN GP09.phx.gbl...
"jimdscudde r" <ji*********@no spam.nospam> wrote in message
news:ul******** ******@TK2MSFTN GP15.phx.gbl... 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:
StringCollectio n propNames = new StringCollectio n();
ManagementClass driveClass = new ManagementClass ("Win32_DiskDri ve");
PropertyDataCol lection props = driveClass.Prop erties;
foreach (PropertyData driveProperty in props)
{
propNames.Add(d riveProperty.Na me);
}
ManagementObjec tCollection drives = driveClass.GetI nstances();
foreach (ManagementObje ct drv in drives)
{
string id = "Index";
this.listBox1.I tems.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_logicaldi sk where drivetype=3" );
ManagementObjec tSearcher searcher = new ManagementObjec tSearcher(query );
foreach (ManagementObje ct mo in searcher.Get())
{
this.listBox1.I tems.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_LogicalDi sk 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_LogicalDi sk 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_Diskdri ve"))
{
ManagementObjec tCollection moc = devs.GetInstanc es();
foreach(Managem entObject mo in moc)
{
Console.WriteLi ne(mo["DeviceId"]);
foreach (ManagementObje ct b in
mo.GetRelated(" Win32_DiskParti tion"))
{
Console.WriteLi ne("{0}", b["Name"]);
foreach (ManagementBase Object c in
b.GetRelated("W in32_LogicalDis k"))
Console.WriteLi ne("{0}", c["Name"]);
}
}
}
Willy.