可用性Win32_MountPoint和Win32_Volume在Windows XP上的? [英] availability of Win32_MountPoint and Win32_Volume on Windows XP?

查看:137
本文介绍了可用性Win32_MountPoint和Win32_Volume在Windows XP上的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

不过,我开发的Windows XP(64位),一个C#应用程序,我可以得到这些WMI类就好了。我的应用程序的用户将在Windows XP SP2与.NET 3.5 SP1。

谷歌搜索左右,我不能确定我是否能在这个或不计。 我是成功的,因为我的一个或多个以下的系统: - Windows XP服务包2? - 的Visual Studio 2008 SP1的安装? - .NET 3.5 SP1?

我应该使用的东西比其他WMI来获得在卷/挂载点信息?

下面是示例code,它的工作...

 公共静态字典<字符串的NameValueCollection> GetAllVolumeDeviceIDs()
    {
        字典<字符串的NameValueCollection> RET =新字典<字符串的NameValueCollection>();

        //从Win32_Volume检索信息
        尝试
        {
            使用(ManagementClass volClass =新ManagementClass(Win32_Volume))
            {
                使用(ManagementObjectCollection mocVols = volClass.GetInstances())
                {
                    //每个卷遍历
                    的foreach(的ManagementObject moVol在mocVols)
                    {
                        //获取卷的设备ID(将钥匙插入我们的字典)
                        字符串DEVID = moVol.GetPropertyValue(的DeviceID)的ToString()。

                        ret.Add(DEVID,新的NameValueCollection());

                        //Console.WriteLine("Vol:{0},DEVID);

                        //有关音量每个非空属性,将其添加到我们的NameValueCollection
                        的foreach(在moVol.Properties PropertyData P)
                        {
                            如果(p.Value == NULL)
                                继续;
                            保留[DEVID]。新增(p.Name,p.Value.ToString());
                            //Console.WriteLine("\t{0}:{1},p.Name,p.Value);
                        }

                        //找到该卷的挂载点
                        使用(ManagementObjectCollection mocMPs = moVol.GetRelationships(Win32_MountPoint))
                        {
                            的foreach(MOMP的ManagementObject在mocMPs)
                            {
                                //只在乎添加目录
                                //目录道具会像Win32_Directory.Name = \C:\\\\\
                                字符串DIR = MOMP [目录]的ToString()。

                                //找到开/关报价,以获得我们想要的子串
                                INT第一= dir.IndexOf('')+ 1;
                                INT最后= dir.LastIndexOf('');
                                字符串dirSubstr = dir.Substring(第一,最后 - 第一);

                                //使用GetFullPath正常化/反向转义的任何额外的反斜线
                                字符串FULLPATH = Path.GetFullPath(dirSubstr);

                                RET [DEVID]。新增(MOUNTPOINT_DIRS_KEY,完整路径);

                            }
                        }
                    }
                }
            }
        }
        赶上(例外前)
        {
            Console.WriteLine(问题从WMI检索卷信息{0}  -  \ N {1},ex.Message,ex.StackTrace);
            返回RET;
        }

        返回RET;

    }
 

解决方案

我猜 Win32_MountPoint Win32_Volume 班可在Windows XP Professional x64 Edition的,因为它是基于Windows Server 2003的codeBase的 。在32位版本的Windows XP,这些类不存在,执行你的任务,你需要的P / Invoke本地卷管理功能,就像蒂姆说。

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;

    }

解决方案

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.

这篇关于可用性Win32_MountPoint和Win32_Volume在Windows XP上的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆