availability of Win32_MountPoint and Win32_Volume

2019-02-25 11:26发布

问题:

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;

    }

回答1:

I guess the Win32_MountPoint and Win32_Volume classes are available on Windows XP Professional x64 Edition because it's based on the Windows Server 2003 codebase. On 32-bit versions of Windows XP, these classes don't exist and to perform your task you need to P/Invoke native volume management functions, like Tim said.



回答2:

You may need to pinvoke into the Win32 Volume Management Functions