我正在使用
启动多个实例sc <server> [command] [service name] <option1> <option2>...
因为我需要在运行时知道服务名称,所以我尝试了
public static string getServiceName(int pid = 0)
{
string result = "";
System.Management.ManagementObjectSearcher mos = new System.Management.ManagementObjectSearcher("root\\CIMV2", string.Format("SELECT * FROM Win32_Service where ProcessId={0}", pid));
System.Management.ManagementObjectCollection moc = mos.Get();
foreach (System.Management.ManagementObject mo in moc)
{
result = Convert.ToString(mo["Name"]);
}
return result;
}
getServiceName(System.Diagnostics.Process.GetCurrentProcess().Id)
没有成功。