Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从c获取windows服务安装文件夹的完整路径#_C#_Visual Studio_Windows Services - Fatal编程技术网

C# 如何从c获取windows服务安装文件夹的完整路径#

C# 如何从c获取windows服务安装文件夹的完整路径#,c#,visual-studio,windows-services,C#,Visual Studio,Windows Services,我用c#开发了一个Winform。我需要获取以前安装的windows服务的完整路径 我可以使用以下代码获取服务的一些属性: ServiceController ctl = new ServiceController("MyCustomService"); service.exe位于此处: C:\Program Files (x86)\Manufacturer\MyCustomService 但我需要从代码中,以友好的方式得到这条路径。。。可能吗 提前感谢…服务控制器类

我用c#开发了一个Winform。我需要获取以前安装的windows服务的完整路径

我可以使用以下代码获取服务的一些属性:

            ServiceController ctl = new ServiceController("MyCustomService");
service.exe位于此处:

C:\Program Files (x86)\Manufacturer\MyCustomService
但我需要从代码中,以友好的方式得到这条路径。。。可能吗


提前感谢…

服务控制器类不会提供windows服务的完整路径,您必须使用WMI或注册表

   WqlObjectQuery wqlObjectQuery = new WqlObjectQuery(string.Format("SELECT * FROM Win32_Service WHERE Name = '{0}'", serviceName));
    ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher(wqlObjectQuery);
    ManagementObjectCollection managementObjectCollection = managementObjectSearcher.Get();

    foreach (ManagementObject managementObject in managementObjectCollection)
    {
        return managementObject.GetPropertyValue("PathName").ToString();
    }

这提供了一个很好的示例,说明如何使用注册表从服务代码中查找执行程序集
System.Reflection.assembly.getExecutionGassembly().Location
的完整服务路径。如果您想从其他应用程序获取路径,可以使用注册表.GetValue()从注册表项
HKEY\U LOCAL\U MACHINE\SYSTEM\CurrentControlSet\Services\YourServiceName
获取
ImagePath
的字符串值,或者在
sc qc YourServiceName
周围制作包装器