2

我想获取 Windows 服务的路径,

var managementObjectSearcher = new ManagementObjectSearcher("Select * from Win32_Service where serviceName = MyService");

这段代码是由其他一些服务的构造函数编写的......

当系统启动并运行时,一切正常,但是如果我重新启动系统,则为这个调用放置 StopWatch 类,它表明它表明服务需要 35-45 秒才能启动,因为这个调用..

任何提高系统重启性能的建议......

4

3 回答 3

7

Windows32_Services 类不存在,因此假设您使用的是Win32_ServiceWMI 类,您可以只返回您想要使用的属性来提高性能,在这种情况下PathName,将您的 WQL 语句更改为

SELECT PathName FROM Win32_Service Where Name='MyService'

更新

@Bacon的观察非常正确,因为您知道要检索的服务的名称,您可以构建 Win32_Service 的对象路径,如下所示

Win32_Service.Name="ServiceName"

然后使用ManagementObject该类,您可以以最快的方式将实例检索到服务中。

于 2012-05-08T17:05:17.963 回答
2

由于您通过 检索服务Name,它是 的关键属性,因此请Win32_Service class尝试直接检索实例而不是搜索它:

string GetMyServicePath()
{
    string path = "Win32_Service.Name=\"MyService\"";

    using (ManagementObject service = new ManagementObject(path))
        return (string) service.GetPropertyValue("PathName");
}

这是我汇总的一个快速基准,用于比较直接检索与搜索:

private const int LoopIterations = 1000;

private const string ServiceClass = "Win32_Service";
private const string ServiceName = "MyService";
private const string ServiceProperty = "PathName";

private static readonly string ServicePath = string.Format("{0}.Name=\"{1}\"", ServiceClass, ServiceName);
private static readonly string ServiceQuery = string.Format(
    "SELECT {0} FROM {1} Where Name=\"{2}\"",
    ServiceProperty, ServiceClass, ServiceName
);
private static ManagementObjectSearcher ServiceSearcher = new ManagementObjectSearcher(ServiceQuery);

static void Main(string[] args)
{
    var watch = new Stopwatch();

    watch.Start();
    for (int i = 0; i < LoopIterations; i++)
    {
        var servicePath = GetServicePathByKey();
    }
    watch.Stop();
    Console.WriteLine(
        "{0:N0} iterations of GetServicePathByKey() took {1:N0} milliseconds",
        LoopIterations, watch.ElapsedMilliseconds
    );

    watch.Restart();
    for (int i = 0; i < LoopIterations; i++)
    {
        var servicePath = GetServicePathFromExistingSearcher();
    }
    watch.Stop();
    Console.WriteLine(
        "{0:N0} iterations of GetServicePathFromExistingSearcher() took {1:N0} milliseconds",
        LoopIterations, watch.ElapsedMilliseconds
    );

    watch.Restart();
    for (int i = 0; i < LoopIterations; i++)
    {
        var servicePath = GetServicePathFromNewSearcher();
    }
    watch.Stop();
    Console.WriteLine(
        "{0:N0} iterations of GetServicePathFromNewSearcher() took {1:N0} milliseconds",
        LoopIterations, watch.ElapsedMilliseconds
    );
}

static string GetServicePathByKey()
{
    using (var service = new ManagementObject(ServicePath))
        return (string) service.GetPropertyValue(ServiceProperty);
}

static string GetServicePathFromExistingSearcher()
{
    using (var results = ServiceSearcher.Get())
    using (var enumerator = results.GetEnumerator())
    {
        if (!enumerator.MoveNext())
            throw new Exception();

        return (string) enumerator.Current.GetPropertyValue(ServiceProperty);
    }
}

static string GetServicePathFromNewSearcher()
{
    using (var searcher = new ManagementObjectSearcher(ServiceQuery))
    using (var results = searcher.Get())
    using (var enumerator = results.GetEnumerator())
    {
        if (!enumerator.MoveNext())
            throw new Exception();

        return (string) enumerator.Current.GetPropertyValue(ServiceProperty);
    }
}

直接枚举搜索结果的速度与我所能达到的速度差不多,比使用foreach块快一点,比使用LINQ. 在我的 64 位 Windows 7 Professional 系统上,ServiceName常量设置为Power我得到了以下结果:

1,000 iterations of GetServicePathByKey() took 8,263 milliseconds
1,000 iterations of GetServicePathFromExistingSearcher() took 64,265 milliseconds
1,000 iterations of GetServicePathFromNewSearcher() took 64,875 milliseconds
于 2012-05-08T19:18:32.607 回答
2

如果是 WMI 的“延迟”困扰您,您不必使用 WMI 来获取服务的路径,即。可执行文件名。您还可以 P/调用QueryServiceConfig

http://www.pinvoke.net/default.aspx/advapi32/queryserviceconfig.html

于 2012-05-08T20:02:29.403 回答