访问Win32_LogicalDisk [英] Accessing Win32_LogicalDisk

查看:128
本文介绍了访问Win32_LogicalDisk的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我目前正在使用WMI编写一个应用程序以检索硬盘信息.通过调查,我发现相关的分类层次结构为:

Win32_DiskDrive
Win32_DiskPartition LogicalDisk

因此,我做了一些foreach循环以获取所需的信息:即:搜索结果的foreach(的ManagementObject硬盘来在HardDriveCollection)结果,{结果的foreach(的ManagementObject HardDiskPartition在HDD.GetRelated(QUOT; Win32_DiskPartition"))结果,{结果的foreach(的ManagementObject逻辑磁盘中分区之前.GetRelated("Win32_LogicalDisk"))
此代码运行良好,我可以为每个物理驱动器获取关联的分区和逻辑驱动器.但是,当我获得相关的Win32_LogicalDisk类时,我的应用程序挂起了大约10秒钟.

经过调查,我发现这与扫描软盘驱动器有关.我需要某种方法来检索类,而不必检索Win32_LogicalDisk的每个实例,因此也不必检索软盘驱动器.

我知道我可以通过该查询查询类中的正确实例-" select * from Win32_LogicalDisk,其中drivetype = 3";但是,使用.GetRelated方法,我只能提供类的名称.

如果有人对我应该解决这个问题的方向有任何想法,我将不胜感激.


br>

Hi Everyone,

I'm currently writing an application using WMI to retrieve Hard Drive information. From investigation i've seen that the hierarchy of the associated clases are:

Win32_DiskDrive
Win32_DiskPartition
Win32_LogicalDisk

Therefore I've done some foreach loops to get the required information: ie:

            foreach (ManagementObject HardDisk in HardDriveCollection)
            {
                foreach (ManagementObject HardDiskPartition in HDD.GetRelated("Win32_DiskPartition"))
                {
                    foreach (ManagementObject LogicalDisk in Parition.GetRelated("Win32_LogicalDisk"))
                    {

This code works perfectly, I can get associated partitions and logical drives for each physical drive. However, when I get the related Win32_LogicalDisk classes my application hangs for about 10 seconds.

After investigation I've found it it's to do with scanning the floppy drive. I need for some way to retrieve the classes without having to retrieve every single instance of Win32_LogicalDisk and consequently the floppy drive.

I know I can query the class for only correct instances by this query - "select * from Win32_LogicalDisk where drivetype = 3" however with the .GetRelated method I can only supply the name of the class.

If anyone has any ideas in the direction i should go to solve this issue I would appreciate it.

Thanks

推荐答案

为什么不循环所有逻辑磁盘,如下所示.您到底想使用GetRelated做什么?

Why not cycle thorugh all logical disks like the below.  What exactly are you trying to do with the GetRelated?

  ManagementClass management = new ManagementClass("root\\CIMV2:Win32_LogicalDisk");
            ManagementObjectCollection col = management.GetInstances();
            foreach (ManagementObject o in col)
            {
                object value = o.GetPropertyValue("DriveType");
                if (value != null)
                {
                    if (Convert.ToInt32(value.ToString()) == 5)
                    {
                        if (o.Properties["VolumeName"].Value != null)
                        {
                            string volDir = o.Properties["DeviceID"].Value.ToString();
                           
                                
                                if (this.OnShowDialog("Would you like to rip the current Disc?", ToolTipIcon.Info) == DialogResult.Yes)
                                {
                                    Console.WriteLine("Disc In");
#if DEBUG

                                    foreach (PropertyData data in o.Properties)
                                    {
                                        if (data.Value == null)
                                        {
                                            Console.WriteLine(data.Name + ": ");
                                        }
                                        else
                                        {
                                            Console.WriteLine(data.Name + ": " + data.Value.ToString());
                                        }
                                    }
#endif
                                     //Do Stuff
                                }
                            
                        }
                        else
                        {
                            Console.WriteLine("No Disc");
                        }
                    }
                }
            }


这篇关于访问Win32_LogicalDisk的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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