Some time we need to get specific service path and then do something you want. there are 2 way to get specific service path bellow.
private static string GetRegistData(string name)
{
string registData;
RegistryKey hkml = Registry.LocalMachine;
RegistryKey system = hkml.OpenSubKey("SYSTEM", true);
RegistryKey currentControlSet = system.OpenSubKey("CurrentControlSet", true);
RegistryKey services = currentControlSet.OpenSubKey("services", true);
RegistryKey key = services.OpenSubKey(name, true);
registData = key.GetValue("ImagePath").ToString();
return registData;
}
private static string GetServicePath(string name)
{
ManagementClass mc = new ManagementClass("Win32_Service");
foreach (ManagementObject mo in mc.GetInstances())
{
if (mo.GetPropertyValue("Name").ToString() == name)
{
return mo.GetPropertyValue("PathName").ToString().Trim('"');
}
}
return string.Empty;
}
本文介绍了如何通过注册表和Windows服务管理类获取特定服务路径的两种方法,并提供了相应的C#代码示例。
8183

被折叠的 条评论
为什么被折叠?



