问题
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