473,624 Members | 2,575 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 24146
>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
7889
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
4442
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
2490
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
1597
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
2002
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
2730
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
1657
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
14465
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
2044
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
8249
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8633
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8348
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,...
1
6112
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
5570
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
4084
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
4187
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2613
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1797
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.