3
是否有可能從服務名稱中獲取進程標識而不使用c#中的管理對象?當WMI服務處於錯誤狀態時,管理對象不起作用。使用c#獲取服務名稱中的進程標識#
是否有可能從服務名稱中獲取進程標識而不使用c#中的管理對象?當WMI服務處於錯誤狀態時,管理對象不起作用。使用c#獲取服務名稱中的進程標識#
希望這會對你有幫助。
SELECT ProcessId FROM Win32_Service WHERE Name='MyServiceName'
試試這個:
Process p = new Process();
p.StartInfo.FileName = "sc.exe";
p.StartInfo.Arguments = "queryex \"" + srv_name + "\"";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.Start();
p.WaitForExit();
string output = p.StandardOutput.ReadToEnd();
p.Close();
if (output.IndexOf("SERVICE_NAME") != -1)
{
bool getPid = false;
string[] words = output.Split(':');
foreach (string word in words)
{
if (word.IndexOf("PID") != -1 && getPid == false)
{
getPid = true;
}
else if (getPid == true)
{
string[] pid = word.Split('\r');
return Convert.ToInt32(pid[0]);
}
}
}