simple managementobject question

D

DBC User

Hi all,

I am in a situation where I need to find the a unc path of a
particular drive or I have a unc path, I need to find the drive.
Either way will work. I do not want to use dllimport to make the win32
api call. So after some research I found out that I could use
ManagementObjects

using (ManagementObjectSearcher DiskSearch =
new ManagementObjectSearcher(new
SelectQuery("Select * from Win32_LogicalDisk")))
{
using (ManagementObjectCollection moDiskCollection
=

DiskSearch.Get())
{
foreach (ManagementObject mo in
moDiskCollection)
{
mo.Dispose();
}
}
}

This works and I was able to find all the drives in the box instead of
going through 'A' thru 'Z', but I did not find the UNC path of these
drives. Does any one know how to go about getting the unc path?

Thanks.
 
W

Willy Denoyette [MVP]

DBC User said:
Hi all,

I am in a situation where I need to find the a unc path of a
particular drive or I have a unc path, I need to find the drive.
Either way will work. I do not want to use dllimport to make the win32
api call. So after some research I found out that I could use
ManagementObjects

using (ManagementObjectSearcher DiskSearch =
new ManagementObjectSearcher(new
SelectQuery("Select * from Win32_LogicalDisk")))
{
using (ManagementObjectCollection moDiskCollection
=

DiskSearch.Get())
{
foreach (ManagementObject mo in
moDiskCollection)
{
mo.Dispose();
}
}
}

This works and I was able to find all the drives in the box instead of
going through 'A' thru 'Z', but I did not find the UNC path of these
drives. Does any one know how to go about getting the unc path?

Thanks.


Check the Win32_NetworkConnection class, it has all properties you are
looking for.


using(ManagementClass netwConn = new
ManagementClass("Win32_NetworkConnection" ))
{
ManagementObjectCollection shares = netwConn.GetInstances();
foreach(ManagementObject share in shares )
Console.WriteLine("{0} - {1}",share["LocalName"],share["RemotePath"]);
}

Check the WMI docs for more details, and use wbemtest.exe to experiment with
WMI before using in your code..


Willy.
 
D

DBC User

I am in a situation where I need to find the a unc path of a
particular drive or I have a unc path, I need to find the drive.
Either way will work. I do not want to use dllimport to make the win32
api call. So after some research I found out that I could use
ManagementObjects
using (ManagementObjectSearcher DiskSearch =
new ManagementObjectSearcher(new
SelectQuery("Select * from Win32_LogicalDisk")))
{
using (ManagementObjectCollection moDiskCollection
=
DiskSearch.Get())
{
foreach (ManagementObject mo in
moDiskCollection)
{
mo.Dispose();
}
}
}
This works and I was able to find all the drives in the box instead of
going through 'A' thru 'Z', but I did not find the UNC path of these
drives. Does any one know how to go about getting the unc path?

Check the Win32_NetworkConnection class, it has all properties you are
looking for.

using(ManagementClass netwConn = new
ManagementClass("Win32_NetworkConnection" ))
{
ManagementObjectCollection shares = netwConn.GetInstances();
foreach(ManagementObject share in shares )
Console.WriteLine("{0} - {1}",share["LocalName"],share["RemotePath"]);
}

Check the WMI docs for more details, and use wbemtest.exe to experiment with
WMI before using in your code..

Willy.- Hide quoted text -

- Show quoted text -

Thank you very much for the answer, it helps.
 

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