【问题标题】:availability of Win32_MountPoint and Win32_Volume on Windows XP?Win32_MountPoint 和 Win32_Volume 在 Windows XP 上的可用性?
【发布时间】:2011-02-18 09:46:57
【问题描述】:

从我发现的 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服务包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;

    }

【问题讨论】:

  • 我在 32 位 XP 机器(XPSP3 w/.NET 3.5Sp1 VS2008SP1)上试用了您的代码,并在 ManagementClass("Win32_Volume") 调用中遇到了“未找到”异常

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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您可能需要拨打Win32 Volume Management Functions

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-15
      • 2011-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多