get drive letter( Win32_Diskdrive)

S

sanjana

hi
i m using the win32_diskdrive class for detecting a
memory card insertion and removal..and this class
detects memory card(sd card..etc) insertion removal from the device

the deviceid attribute gives me \\.\PHYSICALDRIVE0 or
\\.\PHYSICALDRIVE1
etc..

i m trying to get the drive name say(c: or d: ...etc)from the device
id
attribute returned above..

i found this code on the net which said wud get the
drive letter from the device id..

using(ManagementClass 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"]);
}
}
}

but it dint work..it throws an error "NOT FOUND" AT THIS LINE:
foreach (ManagementObject b in mo.GetRelated("Win32_DiskPartition"))

any idea y am i getting this exception?
thanx
 
W

Willy Denoyette [MVP]

sanjana said:
hi
i m using the win32_diskdrive class for detecting a
memory card insertion and removal..and this class
detects memory card(sd card..etc) insertion removal from the device

the deviceid attribute gives me \\.\PHYSICALDRIVE0 or
\\.\PHYSICALDRIVE1
etc..

i m trying to get the drive name say(c: or d: ...etc)from the device
id
attribute returned above..

i found this code on the net which said wud get the
drive letter from the device id..

using(ManagementClass 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"]);
}
}
}

but it dint work..it throws an error "NOT FOUND" AT THIS LINE:
foreach (ManagementObject b in mo.GetRelated("Win32_DiskPartition"))

any idea y am i getting this exception?


Probably because the disk is not partitioned!

Willy.
 
S

sanjana

hi
ya..its working fine..now i am getting the drive letter from the code
thanx...

just a small doubt i have
i get the drive letter whenever i insert the media card in the
particular drive..cf card or sd card..in the drive,..

but i dont get the drive letter when i remove the media card(sd/cf
card)
when i remove the card it gives me /.//physicaldrive1..but doesent map
to the drive letter say (E:)

any reasons for this..?

i had done some cd insertion /removal event detection ..where i used to
get the drive letetr..even if the cd was removed and inserted..
then y does it not work similarly for media cards..

thanx for ur time and advice..







sanjana said:
hi
i m using the win32_diskdrive class for detecting a
memory card insertion and removal..and this class
detects memory card(sd card..etc) insertion removal from the device

the deviceid attribute gives me \\.\PHYSICALDRIVE0 or
\\.\PHYSICALDRIVE1
etc..

i m trying to get the drive name say(c: or d: ...etc)from the device
id
attribute returned above..

i found this code on the net which said wud get the
drive letter from the device id..

using(ManagementClass 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"]);
}
}
}

but it dint work..it throws an error "NOT FOUND" AT THIS LINE:
foreach (ManagementObject b in mo.GetRelated("Win32_DiskPartition"))

any idea y am i getting this exception?


Probably because the disk is not partitioned!

Willy.
 
W

Willy Denoyette [MVP]

sanjana said:
hi
ya..its working fine..now i am getting the drive letter from the code
thanx...

just a small doubt i have
i get the drive letter whenever i insert the media card in the
particular drive..cf card or sd card..in the drive,..

but i dont get the drive letter when i remove the media card(sd/cf
card)
when i remove the card it gives me /.//physicaldrive1..but doesent map
to the drive letter say (E:)

any reasons for this..?

i had done some cd insertion /removal event detection ..where i used to
get the drive letetr..even if the cd was removed and inserted..
then y does it not work similarly for media cards..

thanx for ur time and advice..
The drive letter is asssigned when the card is present and formatted, that
is when a volume is mounted.
CD's are assigned fixed driver letters by the OS even when no CD is present,
memory cards are not.


Willy.
 
S

sanjana

hi
ok
got it
thanx
The drive letter is asssigned when the card is present and formatted, that
is when a volume is mounted.
CD's are assigned fixed driver letters by the OS even when no CD is present,
memory cards are not.


Willy.
 
Joined
Oct 9, 2006
Messages
1
Reaction score
0
Hi,

I am trying to accomplish basically the same thing, but I cannot use the .NET Framework, so I cannot use the ManagementObject classes. I am currently using

hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT * FROM WIN32_DiskDrive"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);


as suggested by the MSDN pages. Basically, I need to get information on the disk drives on my machine, and then I need to be able to access files on those drives. WIN32_DiskDrive will get me the information I need, and WIN32_LogicalDrive will get me the drive letter. Is there any way to map the two?

If anyone could point me in the right direction it would be much appreciated.

Thanks,
-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top