获取进程ID并存储输出,Process

241 阅读1分钟

获取这个名字的进程

使用语言:C#

环境:.net Framework 4.6.1 (当前使用) (貌似支持所有环境,我就不多说了)

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace SearchIdOfProcess
{
    class Program
    {
        static List<int> pid = new List<int>();
        static void Main(string[] args)
        {
            for (int i = 0; i < pid.Count(); i++)
            {
                Console.WriteLine("第{0}个程序的ID:{1}", i, pid[i]);
            }
        }
        public static int GetProcessId()
        {
            //获取所有名叫Jt进程的信息
            Process[] processIdAry = Process.GetProcessesByName("Jt");
            //如果合格集合信息里面有信息
            if (processIdAry.Count() > 0)
            {
                //循环已开启的进程集合
                foreach (var oneId in processIdAry)
                {
                    //用lambda表达式进行 信息处理:(将没添加的ID加入到这里面)
                    if (!(pid.Where(n => n == oneId.Id).ToList().Count() > 0))
                    {
                        pid.Add(oneId.Id);
                    }

                }

            }
            return 0;
        }
    }
}

作者:盗理者
来源:CSDN
原文:blog.csdn.net/qq_36051316…
版权声明:本文为博主原创文章,转载请附上博文链接!