availability of Win32_MountPoint and Win32_Volume on Windows XP?
Posted
by SteveC
on Stack Overflow
See other posts from Stack Overflow
or by SteveC
Published on 2010-05-12T00:07:59Z
Indexed on
2010/05/12
0:14 UTC
Read the original article
Hit count: 686
From the MSDN articles I've found -- http://msdn.microsoft.com/en-us/library/aa394515(v=VS.85).aspx -- Win32_Volume and Win32_MountPoint aren't available on Windows XP.
However, I'm developing a C# app on Windows XP (64bit), and I can get to those WMI classes just fine. Users of my app will be on Windows XP sp2 with .Net 3.5 sp1.
Googling around, I can't determine whether I can count on this or not. Am I successful on my system because of one or more of the following: - windows xp service pack 2? - visual studio 2008 sp1 was installed? - .Net 3.5 sp1?
Should I use something other than WMI to get at the volume/mountpoint info?
Below is sample code that's working...
public static Dictionary<string, NameValueCollection> GetAllVolumeDeviceIDs()
{
Dictionary<string, NameValueCollection> ret = new Dictionary<string, NameValueCollection>();
// retrieve information from Win32_Volume
try
{
using (ManagementClass volClass = new ManagementClass("Win32_Volume"))
{
using (ManagementObjectCollection mocVols = volClass.GetInstances())
{
// iterate over every volume
foreach (ManagementObject moVol in mocVols)
{
// get the volume's device ID (will be key into our dictionary)
string devId = moVol.GetPropertyValue("DeviceID").ToString();
ret.Add(devId, new NameValueCollection());
//Console.WriteLine("Vol: {0}", devId);
// for each non-null property on the Volume, add it to our NameValueCollection
foreach (PropertyData p in moVol.Properties)
{
if (p.Value == null)
continue;
ret[devId].Add(p.Name, p.Value.ToString());
//Console.WriteLine("\t{0}: {1}", p.Name, p.Value);
}
// find the mountpoints of this volume
using (ManagementObjectCollection mocMPs = moVol.GetRelationships("Win32_MountPoint"))
{
foreach (ManagementObject moMP in mocMPs)
{
// only care about adding directory
// Directory prop will be something like "Win32_Directory.Name=\"C:\\\\\""
string dir = moMP["Directory"].ToString();
// find opening/closing quotes in order to get the substring we want
int first = dir.IndexOf('"') + 1;
int last = dir.LastIndexOf('"');
string dirSubstr = dir.Substring(first , last - first);
// use GetFullPath to normalize/unescape any extra backslashes
string fullpath = Path.GetFullPath(dirSubstr);
ret[devId].Add(MOUNTPOINT_DIRS_KEY, fullpath);
}
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("Problem retrieving Volume information from WMI. {0} - \n{1}",ex.Message,ex.StackTrace);
return ret;
}
return ret;
}
© Stack Overflow or respective owner