C#列举结束进程

来源:互联网 发布:树莓派3 ubuntu mate 编辑:程序博客网 时间:2024/06/11 18:32

using System; 
using System.Diagnostics; 
using System.Management; 
//列举进程列表

private static void psst() 
    { 
        Process[] procList = Process.GetProcesses(); 
        Console.WriteLine("{0,-6}{1,-12}{2,-22}{3}","PID","Process","StartTime","Title"); 
        for (int i=0;i<procList.Length-1;i++)         
        {         
            string strProcName = procList[i].ProcessName; 
            int iProcID = procList[i].Id; 
            DateTime dProcStTime = procList[i].StartTime; 
            string strProcTitle = procList[i].MainWindowTitle; 
            Console.WriteLine("{0,-6}{1,-12}{2,-22}{3}",iProcID,strProcName,dProcStTime,strProcTitle); 
        } 
    } 

//显示路径
private static void psph() 
    { 
        Process[] p = Process.GetProcesses(); 
         ManagementObjectSearcher searcher; 
        ManagementObjectCollection collection;     
        searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Process"); 
        collection = searcher.Get(); 
        Console.WriteLine("{0,-5}{1,-16}{2}","PID","ProcName","ProcessPath"); 
        foreach (ManagementObject service in collection) 
        { 
         Console.WriteLine("{0,-5}{1,-16}{2}",service["ProcessID"],service["Name"],service["ExecutablePath"]); 
        } 
     } 

//根据进程名及ID结束进程
private static void kill(string idname) 
       { 
           Process[] all = Process.GetProcesses(); 
           foreach(Process p in all) 
           { 
             if(p.Id.ToString() == idname || p.ProcessName == idname) 
             { 
                  p.Kill(); 
                  p.Close(); 
                  Console.WriteLine("Process {0} was terminated successfully",idname); 
              } 
           } 
       } 

原创粉丝点击