How to get service executable file path

隐身守侯 提交于 2020-02-29 06:18:05

问题


Is there anyways to get the executable file path of a running Windows service without requiring administrator execution level privilege in .Net? (Without causing the UAC to get the user's confirmation)


回答1:


Here's a possible solution using WMI:

using System.Management;


String query = String.Format("SELECT PathName FROM Win32_Service WHERE Name = '{0}'", serviceName);

using (ManagementObjectSearcher mos = new ManagementObjectSearcher(query)) {
    foreach(ManagementObject mo in mos.Get()) 
        Console.WriteLine(mo["PathName"].ToString());
}

Make sure to wrap the ManagementObjectSearcher in a using block because it is a resource and needs to be disposed of properly.

I am not sure if this requires elevated permissions to run, but it does require WMI to be installed and running on the machine. To check if WMI is running, open the services snap-in and look for Windows Management Instrumentation. You can also enable it by running net start winmgmt from the command line.




回答2:


Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)



回答3:


Try getting from Registry:

using Microsoft.Win32;
.
.
.

RegistryKey hklm = Registry.LocalMachine;
hklm = hklm.OpenSubKey(@"System\CurrentControlSet\Services\SERVICE_NAME");

string servicePath = hklm.GetValue("ImagePath");


来源:https://stackoverflow.com/questions/1045065/how-to-get-service-executable-file-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!