如何让只进,只读在C#WMI查询? [英] How to make forward-only, read-only WMI queries in C#?

查看:79
本文介绍了如何让只进,只读在C#WMI查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经告诉一个同事说,如果我的WMI的系统收集信息查询是只进和/或只读,他们会非常快。这就说得通了。但是,我怎么办呢?

I've been told by a coworker that if my WMI system information gathering queries are forward-only and/or read-only, they'll be quite faster. That makes sense. But how do I do it?

推荐答案

您需要使用EnumerationOptions类并将其rewindable的属性设置为false。下面是一个例子:

You need to use EnumerationOptions class and set its Rewindable property to false. Here is an example:

using System;
using System.Management;

namespace WmiTest
{
    class Program
    {
        static void Main()
        {
            EnumerationOptions options = new EnumerationOptions();
            options.Rewindable = false;
            options.ReturnImmediately = true;

            string query = "Select * From Win32_Process";

            ManagementObjectSearcher searcher =
                new ManagementObjectSearcher(@"root\cimv2", query, options);

            ManagementObjectCollection processes = searcher.Get();

            foreach (ManagementObject process in processes)
            {
                Console.WriteLine(process["Name"]);
            }

            // Uncomment any of these
            // and you will get an exception:

            //Console.WriteLine(processes.Count);

            /*
            foreach (ManagementObject process in processes)
            {
                Console.WriteLine(process["Name"]);
            }
            */
        }
    }
}

您将不会看到任何的性能提升,除非你用它来枚举类有大量的实例(如Cim_DataFile),你会得到枚举返回ManagementObjectCollection只有一次。你也将无法使用ManagementObjectCollection.Count等 对于只读查询,我不知道如何使这些。

You won't see any performance improvement unless you use it to enumerate a class with a large number of instances (like Cim_DataFile) and you will get to enumerate the returned ManagementObjectCollection only once. You also won't be able to use ManagementObjectCollection.Count, etc. As for read-only queries, I'm not sure how to make those.

这篇关于如何让只进,只读在C#WMI查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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