C#操纵进程及相关
来源:互联网 发布:af淘宝旗舰店真假 编辑:程序博客网 时间:2024/06/03 22:45
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Diagnostics;//包含头文件namespace ProcessMonitor{ public partial class Form1 : Form { Process[] myProcess; public Form1() { InitializeComponent(); dataGridView1.AllowUserToAddRows = false; dataGridView1.AutoResizeColumns(); dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; dataGridView1.MultiSelect = false; } private void Form1_Load(object sender, EventArgs e) { GetAllProcess();//获取所有进程信息 } private void GetAllProcess()//获取所有进程信息 { dataGridView1.Rows.Clear(); myProcess = Process.GetProcesses(); foreach (Process p in myProcess) { int nextRowIndex = dataGridView1.Rows.Add(); DataGridViewRow row=dataGridView1 .Rows [nextRowIndex]; row.Cells[0].Value = p.Id; row.Cells[1].Value = p.ProcessName; row.Cells[2].Value = string.Format("{0:###,##0.00}MB",p.WorkingSet64/1024.0f/1024.0f); try { row.Cells[3].Value = string.Format("{0}", p.StartTime); row.Cells[4].Value = p.MainModule.FileName; } catch { row.Cells[3].Value = "无法获取"; row.Cells[4].Value = "无法获取"; } } } private void ShowProcessInfo(Process p)//将某个进程的详细信息显示到richtextbox1中 { StringBuilder sb=new StringBuilder (); sb.AppendLine("进程名称:"+p.ProcessName+", ID"+p.Id); try { sb.AppendLine ("进程优先级:"+p.BasePriority+"(优先级类别: "+p.PriorityClass+")"); ProcessModule m=p.MainModule; sb.AppendLine ("文件名:"+m.FileName); sb.AppendLine ("版 本:"+m.FileVersionInfo .FileVersion); sb.AppendLine ("描 述:"+m.FileVersionInfo.FileDescription); sb.AppendLine ("语 言:"+m.FileVersionInfo.Language); sb.AppendLine("======================================"); if (p.Modules!=null) { ProcessModuleCollection pmc=p.Modules; sb.AppendLine("调用的模块(.dll):"); for(int i=1;i<pmc.Count;i++) { sb.AppendLine( "模块名:"+pmc[i].ModuleName+"\t"+ "版 本:"+pmc[i].FileVersionInfo.FileVersion+"\t"+ "描 述:"+pmc[i].FileVersionInfo .FileDescription+"\t" ); } } } catch { sb.AppendLine("无其他信息!"); } this.richTextBox1.Text=sb.ToString(); } private void btnRefresh_Click(object sender, EventArgs e)//刷新进程信息 { GetAllProcess(); } private void dataGridView1_MouseClick(object sender, MouseEventArgs e)//选中进程 { DataGridView.HitTestInfo h = dataGridView1.HitTest(e.X,e.Y); if (h.Type == DataGridViewHitTestType.Cell || h.Type == DataGridViewHitTestType.RowHeader) { dataGridView1.Rows[h.RowIndex].Selected = true; int processID=(int)dataGridView1.CurrentRow.Cells[0].Value; ShowProcessInfo(Process.GetProcessById(processID)); } } private void btnNewProcess_Click(object sender, EventArgs e)//新建记事本进程 { ProcessStartInfo ps = new ProcessStartInfo("NotePad.exe",@"C:\Users\lenovo\Desktop\新建文本文档.txt"); ps.WindowStyle = ProcessWindowStyle.Normal; Process p = new Process(); p.StartInfo = ps; p.Start();//启动进程 //等待启动完成,否则获取进程信息可能会失败 p.WaitForInputIdle(); GetAllProcess(); SelectNewItem(p.Id.ToString().Trim()); }private void btnNewExeProcess_Click(object sender, EventArgs e)//打开Exe文件 { ProcessStartInfo ps = new ProcessStartInfo("BS29A.exe");//打开应用程序BS29A.exe ps.WorkingDirectory = @"C:\Users\lenovo\Desktop\BS2003 V14\BS29A\bin\Debug\"; ps.WindowStyle = ProcessWindowStyle.Normal; Process p = new Process(); p.StartInfo = ps; p.Start();//启动进程 //等待启动完成,否则获取进程信息可能会失败 p.WaitForInputIdle(); GetAllProcess();
SelectNewItem(p.Id.ToString().Trim());
}
private void btnCloseProcess_Click(object sender, EventArgs e)//关闭当前选中进程 { if (dataGridView1.SelectedRows!=null) { int processID = (int)dataGridView1.CurrentRow.Cells[0].Value; Process p = Process.GetProcessById(processID); p.CloseMainWindow(); Thread.Sleep(1000); p.Close(); GetAllProcess(); } } private void KillProcessToolStripMenuItem_Click(object sender, EventArgs e)//快捷菜单中关闭选中进程 { int processID = (int)dataGridView1.CurrentRow.Cells[0].Value; Process p = Process.GetProcessById(processID); p.CloseMainWindow(); Thread.Sleep(1000); p.Close(); GetAllProcess(); } private void SelectNewItem(String processID)//选中刚刚创建的进程信息 { dataGridView1.ClearSelection();//清除所有选择 foreach(DataGridViewRow Row in dataGridView1.Rows ) { if (Row.Cells[0].Value.ToString().CompareTo(processID) == 0) { Row.Selected = true; dataGridView1.CurrentCell = Row.Cells[1];//将当前单元设置为该单元格,从而使其滚动到当前选中行中 return; } } } }}
- C#操纵进程及相关
- C#进程相关方法
- 进程及相关API
- 进程及相关API
- 【linux】后台及进程相关
- Linux进程及相关定义
- 进程终止及等待相关
- c# 数据库操纵封装
- C#字符串操纵总结
- C#操纵Word
- C#操纵IIS.
- C#操纵Word
- 用C#操纵IIS
- c#操纵控制面板
- C# 操纵 IIS 类
- 用C#操纵IIS
- C# 操纵xml
- C#开启及停止进程
- andorid知识点集合
- spring in action Third (第一章)
- 根据二叉树的前序和中序序列来重建二叉树
- HTML&CSS快速入门
- SSE指令介绍及其C、C++应用(2)
- C#操纵进程及相关
- 获取窗口句柄
- 64位WIN7+oracle11g+plsql安装
- Boost.Lambda是什么?
- SQL语句
- Java实现大文件分割
- linux c/c++ 注意
- pythonPkg_xml.etree.ElementTree
- 使用Amoeba for mysql实现mysql读写分离