Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since you're retrieving the service by <code>Name</code>, which a key property of the <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa394418.aspx" rel="nofollow"><code>Win32_Service class</code></a>, try retrieving the instance directly instead of searching for it:</p> <pre><code>string GetMyServicePath() { string path = "Win32_Service.Name=\"MyService\""; using (ManagementObject service = new ManagementObject(path)) return (string) service.GetPropertyValue("PathName"); } </code></pre> <p>Here's a quick benchmark I threw together to compare direct retrieval vs. searching:</p> <pre><code>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 &lt; 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 &lt; 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 &lt; 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); } } </code></pre> <p>Enumerating the searcher results directly is about as fast as I could make it, marginally faster than using a <code>foreach</code> block and twice as fast as using <code>LINQ</code>. On my 64-bit Windows 7 Professional system with the <code>ServiceName</code> constant set to <code>Power</code> I got these results:</p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload