我必须在.Net Admin应用程序中获得Windows服务的绝对路径.我正在使用.Net的ServiceController,如下所示.
ServiceController serviceController = new ServiceController(serviceName);
您可以使用WMI获得此功能,这需要对System.Management的程序集引用:
原文链接:https://www.f2er.com/windows/371624.htmlusing System.Management; class Program { static void Main(string[] args) { Console.WriteLine(GetPathOfService("eventlog")); Console.ReadLine(); } public static string GetPathOfService(string serviceName) { 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(); } return null; } }