finding the actual executable and path associated to a windows service using c#

后端 未结 2 1545
再見小時候
再見小時候 2021-01-12 00:59

I am working on an installation program for one of my company\'s product. The product can be installed multiple times and each installation represents a separate windows ser

相关标签:
2条回答
  • 2021-01-12 01:05

    the interface has changed since @sidprasher answered, try:

    var collection = searcher.Get().Cast<ManagementBaseObject>()
            .Where(mbo => mbo.GetPropertyValue("StartMode")!=null)
            .Select(mbo => Tuple.Create((string)mbo.GetPropertyValue("Name"), (string)mbo.GetPropertyValue("PathName")));
    
    0 讨论(0)
  • 2021-01-12 01:18

    I might be wrong but the ServiceController class doesn't provide that information directly.

    So as suggested by Gene - you will have to use the registry or WMI.

    For an example of how to use the registry, refer to http://www.codeproject.com/Articles/26533/A-ServiceController-Class-that-Contains-the-Path-t

    If you decide to use WMI (which I would prefer),

    ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Service");
    ManagementObjectCollection collection = searcher.Get();
    
    foreach (ManagementObject obj in collection)
    {    
        string name = obj["Name"] as string;
        string pathName = obj["PathName"] as string;
        ...
    }
    

    You can decide to wrap the properties you need in a class.

    0 讨论(0)
提交回复
热议问题