简体   繁体   English

WPF WMI Win32_Service查询

[英]Wpf WMI Win32_Service Query

In my WPF application I need to get a list of Windows Services(~200) with a specific properties. 在我的WPF应用程序中,我需要获取具有特定属性的Windows Services(〜200)列表。

                var result = new List<ServicesModel>();                    
                ConnectionOptions connOptions = new ConnectionOptions
                {
                    Impersonation = ImpersonationLevel.Impersonate,
                    EnablePrivileges = true,
                };

                ManagementScope manScope = new ManagementScope($@"\\{System.Environment.MachineName}\ROOT\CIMV2",
                    connOptions);
                manScope.Connect();

                SelectQuery query = new SelectQuery("SELECT * FROM Win32_Service");

                using (var searcher = new ManagementObjectSearcher(manScope, query))
                    foreach (var o in searcher.Get())
                    {
                        var obj = (ManagementObject) o;
                        ServicesModel service = new ServicesModel
                        {
                            Name = obj["DisplayName"] as string,
                            Path = obj["PathName"] as string,
                            Description = obj["Description"] as string,
                            Pid = Convert.ToInt32(obj["ProcessId"]),
                            Status = obj["State"] as string,
                            StartMode = obj["StartMode"] as string,
                            LogOnAs = obj["StartName"] as string
                        };
                        result.Add(service);
                    }
                return result;

It takes approximately ~1 minute to execute this method and return data which is unacceptable. 执行此方法大约需要1分钟,并返回不可接受的数据。 Looks like searcher.Get() takes all that time... What I can do to improve execute/return time/performance? 看起来searcher.Get()花费了所有时间...我可以做些什么来改善执行/返回时间/性能?

Thanks 谢谢

Based on the comments I came up with following, hope it might help someone: 根据我提出的意见,希望它能对某人有所帮助:

 public List<string> GetWindowsServicesList()
    {
        var result = new List<string>();
        foreach (ServiceController service in ServiceController.GetServices())
        {
            string serviceName = service.ServiceName;
            result.Add(serviceName);
        }
        return result;
    }

    public ServicesModel GetServiceProperties(string serviceName)
    {
        ServicesModel service = null;
        string path = $"Win32_Service.Name=\"{serviceName}\"";

        using (ManagementObject mngObj = new ManagementObject(path))
        {
            service = new ServicesModel
            {
                Name = mngObj.GetPropertyValue("Name") as string,
                Path = mngObj.GetPropertyValue("PathName") as string,
                Description = mngObj.GetPropertyValue("Description") as string,
                Pid = Convert.ToInt32(mngObj.GetPropertyValue("ProcessId")),
                Status = mngObj.GetPropertyValue("State") as string,
                StartMode = mngObj.GetPropertyValue("StartMode") as string,
                LogOnAs = mngObj.GetPropertyValue("StartName") as string
            };
        }
        return service;
    }

    public List<ServicesModel> WindowsServicesCollection()
    {
        var result = new List<ServicesModel>();
        try
        {
            var windowsServicesNames = GetWindowsServicesList();

            for (int i = 0; i < windowsServicesNames.Count(); i++)
            {
                result.Add(GetServiceProperties(windowsServicesNames[i]));
            }
        }
        catch (System.Management.ManagementException ex)
        {
        }
        catch (System.TimeoutException ex)
        {
        }
        return result;
    }

Works much, much faster than previous solution although it seems like performance is not super consistent and it depends from some various factors that I did not identified... 尽管看起来性能不是超级一致,并且取决于我未确定的一些因素,但它的工作速度比以前的解决方案快得多,而且要快得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM