我得在.NET管理应用程序窗口服务的绝对路径。我使用的.Net中的ServiceController如下图所示。
I have to get the absolute path of a windows service in a .Net Admin application. I am using ServiceController of .Net as shown below.
ServiceController serviceController = new ServiceController(serviceName);
不过,我看不出有任何财产这里获取服务的.exe文件的绝对路径。反正是有以编程方式获得此。
But I don't see any property here to get the absolute path of the .exe of the service. Is there anyway to get this programmatically.
您可以得到这个使用WMI:
You can get this using WMI:
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;
}
}