我最亲爱的说

子非鱼,焉知鱼之乐.

导航

根据进程名获取PID

/*
 * Created by SharpDevelop.
 * User: Wzqa
 * Date: 2012/12/2
 * Time: 14:08
 * 
 * 根据进程名获取PID.
 */
using System;
using System.Diagnostics;

namespace PID
{
    class Program
    {
        public static void Main(string[] args)
        {
            Console.Write(GetPidByProcessName("1"));
            //不用加后缀名  ".exe"
            Console.ReadKey(true);
        }
        
        public static int GetPidByProcessName(string processName)
        {
            Process[] arrayProcess = Process.GetProcessesByName(processName);

            foreach (Process p in arrayProcess)
            {
                return p.Id;
            }
            return 0;
        }
    }
}

 

posted on 2012-12-02 14:30  我最亲爱的说  阅读(1495)  评论(0编辑  收藏  举报