獲取程序ID並存儲輸出,Process
阿新 • • 發佈:2019-01-26
獲取這個名字的程序
使用語言: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;
}
}
}