2015年8月31日 星期一

處理序(Process)資訊

1. 取得進程資訊
private void getProcessInfo()
{
    try
    {
        listView1.Items.Clear();
        Process[] MyProcesses = Process.GetProcesses();              //取得所有處理程式
        tsslInfo.Text = "進程總數:" + MyProcesses.Length.ToString()  //顯示處理程序總數
        string[] Minfo = new string[6];
        foreach(Process MyProcess in Myprocesses)
        {
            Minfo[0] = MyProcess.ProcessName;                                        //程式名稱
            Minfo[1] = MyProcess.MainModule.ModualName;                              //程式模組
            Minfo[2] = MyProcess.Threads.Count.ToString();                           //程式執行緒數
            Minfo[3] = MyProcess.BasePriority.ToString();                            //程式優先順序
            Minfo[4] = Convert.ToString(MyProcess.WorkingSet / 1024 ) + "K";         //實體記憶體
            Minfo[5] = Convert.ToString(MyProcess.VirtualMemorySize / 1024 ) + "K";  //虛擬記憶體
            ListViewItem lvi = new ListViewItem(Minfo, "process");                   //新增 ListViewItem 物件
            listView1.Items.Add(lvi);
        }
    }
    catch{}
}

2. 結束進程
private void endProcess()
{
    try
    {
        string ProcessName = listView1.SelectItems[0].Text;
        Process[] MyProcess = Process.GetProcessesByName(ProcessName);                   //根據程式名稱構建程式陣列
        MyProcess[0].Kill();                                                             //結束程式
    }
    catch
    {
        string ProcessName = listView1.SelectItems[0].Text;
        Process[] MyProcess1 = Process.GetProcessesByName(ProcessName);                  //根據程式名稱構建程式陣列
        
        MyProcess1[0].StartInfo.FileName = "cmd.exe";                                    //設定程式名
        MyProcess1[0].StartInfo.UserShellExecute = false;                                //關閉Shell的使用
        MyProcess1[0].StartInfo.RedirectStandardInput = ture;                            //重定向標準輸入
        MyProcess1[0].StartInfo.RedircetStandardOutput = true;                           //重定向標準輸出
        MyProcess1[0].StartInfo.RedirectStardardError = true;                            //重定向錯誤輸出
        MyProcess1[0].StartInfo.CreateNoWindows = true;                                  //設定不顯示視窗

        MyProcess1[0].Start();   

    }
}

3. 設定優先權
private void SetBasePriority()
{
    string ProcessName = listView1.SelectItems[0].Text;
    Process[] MyProcess = Process.GetProcessesByName(ProcessName);                   //根據程式名稱構建程式陣列
    switch(i)
    {
        case 0: MyProcess[0].PriorityClass = ProcessPriorityClass.Idle; break;        //低優先順序   
        case 1: MyProcess[0].PriorityClass = ProcessPriorityClass.Normal; break;      //標準優先順序  
        case 2: MyProcess[0].PriorityClass = ProcessPriorityClass.High; break;        //高優先順序  
        case 3: MyProcess[0].PriorityClass = ProcessPriorityClass.RealTime; break;    //即時優先順序  
        case 4: MyProcess[0].PriorityClass = ProcessPriorityClass.AboveNormal; break; //標準以上優先順序 
        case 5: MyProcess[0].PriorityClass = ProcessPriorityClass.BelowNormal; break; //標準以下優先順序       
    }
}

沒有留言:

張貼留言