# Wednesday, March 23, 2005
刚学C#写的一个小程序,来列举本地的进程,两种模式:
1.列举出进程的starttime和title。
2.列举出进程的执行文件完全路径。
编译环境:Windows 2003+Microsoft.NET Framework v1.1.4322
附上源码:

程序代码:

//List all processes on local system(two modes) 

//Kill the specified process 

//By sfzhi at 24/Sep/2004 

using System; 
using System.Diagnostics; 
using System.Management; 
public class PK 

    public static void Main(string[] args) 
    { 
       if(args.Length != 1) 
       { 
        help(); 
       } 
       else 
       { 
        switch(args[0]) 
        { 
         case "/t"
          psst(); 
         break
         case "/p"
          psph(); 
         break
         default
          kill(args[0]); 
         break
        } 
       } 
    } 
    //Show the Usage 

    private static void help() 
    { 
     Console.WriteLine("Local System Process Lister and Killer V1.0 (2004-9-25 C#) "); 
     Console.WriteLine("Code by sfzhi msn:idazhi@hotmail.com"); 
     Console.WriteLine(); 
     Console.WriteLine("Usage:    pk    "); 
     Console.WriteLine("Options:  /t         - Show the starttime and title of all processes"); 
     Console.WriteLine("          /p         - Show the path to executable file of all processes"); 
     Console.WriteLine("          PID/Name   - Kill the specified process"); 
    } 
    //Show the starttime and title of all processes 

    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); 
        } 
    } 
    //Show the path to executable file of all processes 

    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"]); 
        } 
     } 
     //Terminate the specified process 

     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); 
              } 
           } 
       } 
Comments are closed.