Windows XP上Win32_MountPoint和Win32_Volume的可用性?

时间:2010-05-12 00:08:00

标签: c# .net .net-3.5 windows-xp wmi

从我发现的MSDN文章 - http://msdn.microsoft.com/en-us/library/aa394515(v=VS.85).aspx - Win32_Volume和Win32_MountPoint在Windows XP上不可用。

但是,我正在Windows XP(64位)上开发一个C#应用程序,我可以很好地使用这些WMI类。我的应用程序的用户将使用.Net 3.5 sp1在Windows XP SP2上。

谷歌搜索,我无法确定我是否可以指望这一点。 我是否因为以下一项或多项原因在我的系统上取得了成功: - windows xp service pack 2? - visual studio 2008 sp1安装? - .Net 3.5 sp1?

我应该使用WMI之外的其他内容来获取音量/挂载点信息吗?

以下是正在运行的示例代码......

    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;

    }

2 个答案:

答案 0 :(得分:1)

您可能需要转入Win32 Volume Management Functions

答案 1 :(得分:1)

我认为Win32_MountPointWin32_Volume类在Windows XP Professional x64 Edition上可用,因为它是based on the Windows Server 2003 codebase。在32位版本的Windows XP上,这些类不存在,并且要执行您需要P / Invoke本机卷管理功能,如Tim所说。