.net 操作进程 查看所有进程、查找进程、杀进程

 /// <summary>
    /// 系统进程操作
    /// </summary>
    public class GSProcess
    {

        public GSProcess() { }

        /// <summary>
        /// 获取操作系统进程列表
        /// </summary>
        public Dictionary<string, string> GetAllProcess()
        {
            Process[] ps = Process.GetProcesses();
            Dictionary<string, string> di = new Dictionary<string, string>();
            int i = 0;

            StringBuilder sb = new StringBuilder();
            foreach (Process p in ps)
            {
                if (p.MainWindowHandle != null)
                {
                    di.Add(p.Id.ToString(), p.ProcessName);
                    sb.Append("ID: " + p.Id.ToString() + "  ProcessName  " + p.ProcessName);
                }
            }
            return di;
        }

        /// <summary>
        /// 是否存在某种进程
        /// </summary>
        /// <param name="processname"></param>
        /// <returns></returns>
        public bool isExist(string processname)
        {
            Dictionary<string, string> di = new Dictionary<string, string>();
            di = GetAllProcess();

            return di.ContainsValue(processname);
        }

        /// <summary>
        /// 杀进程
        /// </summary>
        /// <param name="processname"></param>
        public void Kill(string processname)
        {
            Process[] ps = Process.GetProcesses();
            foreach (Process p in ps)
            {
                if (p.MainWindowHandle != null && p.ProcessName.Equals(processname))
                {
                    p.Kill();
                }
            }
        }
    }

 

    原文作者:小目标一个亿
    原文地址: https://blog.csdn.net/wwwwerewrew/article/details/117329845
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞