473,695 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

WMI Hard Drive physical location and drive letter problem

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?


Nov 17 '05 #1
7 24156
>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_DiskDrive PhysicalMedia instances? It does
that mapping for you.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #2

"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.
Nov 17 '05 #3
Not really, Win32_DiskDrive PhysicalMedia does the mapping PhysicalMedia -
DiskDrive , not DiskDrive - LogicalDisk.

Willy.

"Mattias Sjögren" <ma************ ********@mvps.o rg> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
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_DiskDrive PhysicalMedia instances? It does
that mapping for you.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 17 '05 #4
With the Win32_DiskDrive PhysicalMedia query you get:
\\UserName\root \cimv2:Win32_Ph ysicalMedia.Tag ="\\\\.\\PHYSIC ALDRIVE0"
\\UserName\root \cimv2:Win32_Di skDrive.DeviceI D="\\\\.\\PHYSI CALDRIVE0"
\\UserName\root \cimv2:Win32_Ph ysicalMedia.Tag ="\\\\.\\PHYSIC ALDRIVE1"
\\UserName\root \cimv2:Win32_Di skDrive.DeviceI D="\\\\.\\PHYSI CALDRIVE1"

Thanks Jim
"Willy Denoyette [MVP]" <wi************ *@telenet.be> wrote in message
news:eV******** ******@TK2MSFTN GP09.phx.gbl...
Not really, Win32_DiskDrive PhysicalMedia does the mapping PhysicalMedia -
DiskDrive , not DiskDrive - LogicalDisk.

Willy.

"Mattias Sjögren" <ma************ ********@mvps.o rg> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
>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_DiskDrive PhysicalMedia instances? It does
that mapping for you.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Nov 17 '05 #5
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.

Nov 17 '05 #6
Not really, Win32_DiskDrive PhysicalMedia does the mapping PhysicalMedia -
DiskDrive , not DiskDrive - LogicalDisk.


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

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #7
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 [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.

Nov 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
7893
by: Kamlesh | last post by:
Hi, How do I know the physical database path of a database. When I goto the DB2INSTANCE users's directory (/home/db2inst1), I see following folders: /db2inst1/NODE0000/SQL00001 /db2inst1/NODE0000/SQL00002 /db2inst1/NODE0000/SQL00003 /db2inst1/NODE0000/SQL00004
1
4443
by: Dani Peer | last post by:
Hi, I build a COM interop DLL. I want that this dll will be able to get it's PHYSICAL LOCATION since I need it to read an XML file. How can I get the Physical location. Directory.GetCurrentDirectory() returns working directory. Any solutions ? Dani
0
2495
by: john doe | last post by:
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);
1
1599
by: Ric Pullen | last post by:
Is it possible to find the physical location of the web page within code? I need to load up a crystal.net report (not embebded in the application) via a filename and i know that the file resides in one directory below the web page. Therefore rather than hard code the full path i want to derive it from the webpage. Cheers
4
2007
by: Verde | last post by:
I know this is somewhat OT, but don't know where else to ask: How can I discover the rough physical location of an IP address? By "rough physical location" I'm thinking at least country, and ideally the state and/or city name. Thanks
1
2732
by: Raed Sawalha | last post by:
I have a problem in getting asp.net application physical path in global.ascx static function in my global.ascx i have a static function : first i tried to get the physical path by refelection using string rootPath = Path.GetDirectoryName(System.Refelection.Assmebly.GetExecutingAssembly().Location); but the problem it returned a path in temprary folder in /winnt/framework/ver/temporay asp.net files ..i do not know why?
2
1660
by: ad | last post by:
I am use a Database name "myBase" of SqlExpress 2005 as DataBase. How can I get the physical location of "myBase"
4
14472
by: Zeeshan | last post by:
hi, i want to get the hard drive number for example if if have drive letter C it should tell me Disk number as 1 and suppose if i have another hard disk attach to my system having letter J, the by giving J it should give me disk number 2. Any kind of help will be fuly appreciated Regards, Zeeshan
4
2047
by: turtle | last post by:
This may be so elementary that no one has ever had to ask it. How do I copy an Excel workbook that is on drive E to drive F using Visual Basic 6.0? I have tried several different ways without success. I don't get error messages, it just doesn't happen. Thanks very much, Turtle
0
8553
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9112
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8822
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7650
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6483
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5827
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4332
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4570
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2251
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.