Csharp/C#教程:使用c#从服务名称获取进程ID分享


使用c#从服务名称获取进程ID

是否有可能在不使用c#中的管理对象的情况下从服务名称获取进程ID? 当WMI服务处于错误状态时,管理对象不起作用。

希望这会帮助你。

SELECT ProcessId FROM Win32_Service WHERE Name='MyServiceName' 

试试这个:

上述就是C#学习教程:使用c#从服务名称获取进程ID分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 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]); } } } 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/948934.html

(0)
上一篇 2021年11月17日
下一篇 2021年11月17日

精彩推荐