利用C#實(shí)現(xiàn)進(jìn)程管理器
更新時(shí)間:2022年12月14日 08:26:28 作者:芝麻粒兒
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)自己的進(jìn)程管理器,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以了解一下
實(shí)踐過程
效果

代碼
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void getProcessInfo()
{
try
{
listView1.Items.Clear();
Process[] MyProcesses = Process.GetProcesses();
tsslInfo.Text = "進(jìn)程總數(shù):" + MyProcesses.Length.ToString();
string[] Minfo = new string[6];
foreach (Process MyProcess in MyProcesses)
{
Minfo[0] = MyProcess.ProcessName;
Minfo[1] = MyProcess.MainModule.ModuleName;
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");
listView1.Items.Add(lvi);
}
}
catch { }
}
private void Form1_Load(object sender, EventArgs e)
{
getProcessInfo();
}
private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
{
getProcessInfo();
}
private void 結(jié)束進(jìn)程ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
if (MessageBox.Show("警告:終止進(jìn)程會(huì)導(dǎo)致不希望發(fā)生的結(jié)果,\r包括數(shù)據(jù)丟失和系統(tǒng)不穩(wěn)定。在被終止前,\r進(jìn)程將沒有機(jī)會(huì)保存其狀態(tài)和數(shù)據(jù)。確實(shí)\r想終止該進(jìn)程嗎?", "任務(wù)管理器警告", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
{
string ProcessName = listView1.SelectedItems[0].Text;
Process[] MyProcess = Process.GetProcessesByName(ProcessName);
MyProcess[0].Kill();
getProcessInfo();
}
else
{ }
}
catch
{
string ProcessName = listView1.SelectedItems[0].Text;
Process[] MyProcess1 = Process.GetProcessesByName(ProcessName);
Process MyProcess = new Process();
//設(shè)定程序名
MyProcess.StartInfo.FileName = "cmd.exe";
//關(guān)閉Shell的使用
MyProcess.StartInfo.UseShellExecute = false;
//重定向標(biāo)準(zhǔn)輸入
MyProcess.StartInfo.RedirectStandardInput = true;
//重定向標(biāo)準(zhǔn)輸出
MyProcess.StartInfo.RedirectStandardOutput = true;
//重定向錯(cuò)誤輸出
MyProcess.StartInfo.RedirectStandardError = true;
//設(shè)置不顯示窗口
MyProcess.StartInfo.CreateNoWindow = true;
//執(zhí)行強(qiáng)制結(jié)束命令
MyProcess.Start();
MyProcess.StandardInput.WriteLine("ntsd -c q -p " + (MyProcess1[0].Id).ToString());
MyProcess.StandardInput.WriteLine("Exit");
getProcessInfo();
}
}
private void SetBasePriority(int i)
{
string ProcessName = listView1.SelectedItems[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;//標(biāo)準(zhǔn)
case 2: MyProcess[0].PriorityClass = ProcessPriorityClass.High; break;//高
case 3: MyProcess[0].PriorityClass = ProcessPriorityClass.RealTime; break;//實(shí)時(shí)
case 4: MyProcess[0].PriorityClass = ProcessPriorityClass.AboveNormal; break;//高于標(biāo)準(zhǔn)
case 5: MyProcess[0].PriorityClass = ProcessPriorityClass.BelowNormal; break;//低于標(biāo)準(zhǔn)
}
getProcessInfo();
}
private void 實(shí)時(shí)ToolStripMenuItem_Click(object sender, EventArgs e)
{
SetBasePriority(3);
}
private void 高ToolStripMenuItem_Click(object sender, EventArgs e)
{
SetBasePriority(2);
}
private void 高于標(biāo)準(zhǔn)ToolStripMenuItem_Click(object sender, EventArgs e)
{
SetBasePriority(4);
}
private void 標(biāo)準(zhǔn)ToolStripMenuItem_Click(object sender, EventArgs e)
{
SetBasePriority(1);
}
private void 低于標(biāo)準(zhǔn)ToolStripMenuItem_Click(object sender, EventArgs e)
{
SetBasePriority(5);
}
private void 低ToolStripMenuItem_Click(object sender, EventArgs e)
{
SetBasePriority(0);
}
}
partial class Form1
{
/// <summary>
/// 必需的設(shè)計(jì)器變量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
/// <param name="disposing">如果應(yīng)釋放托管資源,為 true;否則為 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗體設(shè)計(jì)器生成的代碼
/// <summary>
/// 設(shè)計(jì)器支持所需的方法 - 不要
/// 使用代碼編輯器修改此方法的內(nèi)容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
this.刷新ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.結(jié)束進(jìn)程ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.設(shè)置優(yōu)先級(jí)ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.實(shí)時(shí)ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.高ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.高于標(biāo)準(zhǔn)ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.標(biāo)準(zhǔn)ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.低于標(biāo)準(zhǔn)ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.低ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.tabControl1 = new System.Windows.Forms.TabControl();
this.tabPage1 = new System.Windows.Forms.TabPage();
this.listView1 = new System.Windows.Forms.ListView();
this.columnHeader11 = new System.Windows.Forms.ColumnHeader();
this.columnHeader12 = new System.Windows.Forms.ColumnHeader();
this.columnHeader13 = new System.Windows.Forms.ColumnHeader();
this.columnHeader14 = new System.Windows.Forms.ColumnHeader();
this.columnHeader15 = new System.Windows.Forms.ColumnHeader();
this.columnHeader16 = new System.Windows.Forms.ColumnHeader();
this.statusStrip1 = new System.Windows.Forms.StatusStrip();
this.tsslInfo = new System.Windows.Forms.ToolStripStatusLabel();
this.contextMenuStrip1.SuspendLayout();
this.tabControl1.SuspendLayout();
this.tabPage1.SuspendLayout();
this.statusStrip1.SuspendLayout();
this.SuspendLayout();
//
// contextMenuStrip1
//
this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.刷新ToolStripMenuItem,
this.結(jié)束進(jìn)程ToolStripMenuItem,
this.設(shè)置優(yōu)先級(jí)ToolStripMenuItem});
this.contextMenuStrip1.Name = "contextMenuStrip1";
this.contextMenuStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System;
this.contextMenuStrip1.ShowImageMargin = false;
this.contextMenuStrip1.ShowItemToolTips = false;
this.contextMenuStrip1.Size = new System.Drawing.Size(106, 70);
//
// 刷新ToolStripMenuItem
//
this.刷新ToolStripMenuItem.Name = "刷新ToolStripMenuItem";
this.刷新ToolStripMenuItem.Size = new System.Drawing.Size(105, 22);
this.刷新ToolStripMenuItem.Text = "刷新";
this.刷新ToolStripMenuItem.Click += new System.EventHandler(this.刷新ToolStripMenuItem_Click);
//
// 結(jié)束進(jìn)程ToolStripMenuItem
//
this.結(jié)束進(jìn)程ToolStripMenuItem.Name = "結(jié)束進(jìn)程ToolStripMenuItem";
this.結(jié)束進(jìn)程ToolStripMenuItem.Size = new System.Drawing.Size(105, 22);
this.結(jié)束進(jìn)程ToolStripMenuItem.Text = "結(jié)束進(jìn)程";
this.結(jié)束進(jìn)程ToolStripMenuItem.Click += new System.EventHandler(this.結(jié)束進(jìn)程ToolStripMenuItem_Click);
//
// 設(shè)置優(yōu)先級(jí)ToolStripMenuItem
//
this.設(shè)置優(yōu)先級(jí)ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.實(shí)時(shí)ToolStripMenuItem,
this.高ToolStripMenuItem,
this.高于標(biāo)準(zhǔn)ToolStripMenuItem,
this.標(biāo)準(zhǔn)ToolStripMenuItem,
this.低于標(biāo)準(zhǔn)ToolStripMenuItem,
this.低ToolStripMenuItem});
this.設(shè)置優(yōu)先級(jí)ToolStripMenuItem.Name = "設(shè)置優(yōu)先級(jí)ToolStripMenuItem";
this.設(shè)置優(yōu)先級(jí)ToolStripMenuItem.Size = new System.Drawing.Size(105, 22);
this.設(shè)置優(yōu)先級(jí)ToolStripMenuItem.Text = "設(shè)置優(yōu)先級(jí)";
//
// 實(shí)時(shí)ToolStripMenuItem
//
this.實(shí)時(shí)ToolStripMenuItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
this.實(shí)時(shí)ToolStripMenuItem.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.實(shí)時(shí)ToolStripMenuItem.ImageScaling = System.Windows.Forms.ToolStripItemImageScaling.None;
this.實(shí)時(shí)ToolStripMenuItem.Name = "實(shí)時(shí)ToolStripMenuItem";
this.實(shí)時(shí)ToolStripMenuItem.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.實(shí)時(shí)ToolStripMenuItem.Size = new System.Drawing.Size(118, 22);
this.實(shí)時(shí)ToolStripMenuItem.Text = "實(shí)時(shí)";
this.實(shí)時(shí)ToolStripMenuItem.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.實(shí)時(shí)ToolStripMenuItem.Click += new System.EventHandler(this.實(shí)時(shí)ToolStripMenuItem_Click);
//
// 高ToolStripMenuItem
//
this.高ToolStripMenuItem.Name = "高ToolStripMenuItem";
this.高ToolStripMenuItem.Size = new System.Drawing.Size(118, 22);
this.高ToolStripMenuItem.Text = "高";
this.高ToolStripMenuItem.Click += new System.EventHandler(this.高ToolStripMenuItem_Click);
//
// 高于標(biāo)準(zhǔn)ToolStripMenuItem
//
this.高于標(biāo)準(zhǔn)ToolStripMenuItem.Name = "高于標(biāo)準(zhǔn)ToolStripMenuItem";
this.高于標(biāo)準(zhǔn)ToolStripMenuItem.Size = new System.Drawing.Size(118, 22);
this.高于標(biāo)準(zhǔn)ToolStripMenuItem.Text = "高于標(biāo)準(zhǔn)";
this.高于標(biāo)準(zhǔn)ToolStripMenuItem.Click += new System.EventHandler(this.高于標(biāo)準(zhǔn)ToolStripMenuItem_Click);
//
// 標(biāo)準(zhǔn)ToolStripMenuItem
//
this.標(biāo)準(zhǔn)ToolStripMenuItem.Name = "標(biāo)準(zhǔn)ToolStripMenuItem";
this.標(biāo)準(zhǔn)ToolStripMenuItem.Size = new System.Drawing.Size(118, 22);
this.標(biāo)準(zhǔn)ToolStripMenuItem.Text = "標(biāo)準(zhǔn)";
this.標(biāo)準(zhǔn)ToolStripMenuItem.Click += new System.EventHandler(this.標(biāo)準(zhǔn)ToolStripMenuItem_Click);
//
// 低于標(biāo)準(zhǔn)ToolStripMenuItem
//
this.低于標(biāo)準(zhǔn)ToolStripMenuItem.Name = "低于標(biāo)準(zhǔn)ToolStripMenuItem";
this.低于標(biāo)準(zhǔn)ToolStripMenuItem.Size = new System.Drawing.Size(118, 22);
this.低于標(biāo)準(zhǔn)ToolStripMenuItem.Text = "低于標(biāo)準(zhǔn)";
this.低于標(biāo)準(zhǔn)ToolStripMenuItem.Click += new System.EventHandler(this.低于標(biāo)準(zhǔn)ToolStripMenuItem_Click);
//
// 低ToolStripMenuItem
//
this.低ToolStripMenuItem.Name = "低ToolStripMenuItem";
this.低ToolStripMenuItem.Size = new System.Drawing.Size(118, 22);
this.低ToolStripMenuItem.Text = "低";
this.低ToolStripMenuItem.Click += new System.EventHandler(this.低ToolStripMenuItem_Click);
//
// tabControl1
//
this.tabControl1.Controls.Add(this.tabPage1);
this.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill;
this.tabControl1.Location = new System.Drawing.Point(0, 0);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(469, 314);
this.tabControl1.TabIndex = 6;
//
// tabPage1
//
this.tabPage1.Controls.Add(this.listView1);
this.tabPage1.Location = new System.Drawing.Point(4, 21);
this.tabPage1.Name = "tabPage1";
this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
this.tabPage1.Size = new System.Drawing.Size(461, 289);
this.tabPage1.TabIndex = 0;
this.tabPage1.Text = "進(jìn)程";
this.tabPage1.UseVisualStyleBackColor = true;
//
// listView1
//
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeader11,
this.columnHeader12,
this.columnHeader13,
this.columnHeader14,
this.columnHeader15,
this.columnHeader16});
this.listView1.ContextMenuStrip = this.contextMenuStrip1;
this.listView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.listView1.FullRowSelect = true;
this.listView1.GridLines = true;
this.listView1.Location = new System.Drawing.Point(3, 3);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(455, 283);
this.listView1.TabIndex = 0;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.Details;
//
// columnHeader11
//
this.columnHeader11.Text = "映像名稱";
this.columnHeader11.Width = 100;
//
// columnHeader12
//
this.columnHeader12.Text = "進(jìn)程ID";
this.columnHeader12.Width = 70;
//
// columnHeader13
//
this.columnHeader13.Text = "線程數(shù)";
this.columnHeader13.Width = 70;
//
// columnHeader14
//
this.columnHeader14.Text = "優(yōu)先級(jí)";
//
// columnHeader15
//
this.columnHeader15.Text = "物理內(nèi)存";
this.columnHeader15.Width = 65;
//
// columnHeader16
//
this.columnHeader16.Text = "虛擬內(nèi)存";
this.columnHeader16.Width = 85;
//
// statusStrip1
//
this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.tsslInfo});
this.statusStrip1.Location = new System.Drawing.Point(0, 314);
this.statusStrip1.Name = "statusStrip1";
this.statusStrip1.Size = new System.Drawing.Size(469, 22);
this.statusStrip1.TabIndex = 7;
this.statusStrip1.Text = "statusStrip1";
//
// tsslInfo
//
this.tsslInfo.Name = "tsslInfo";
this.tsslInfo.Size = new System.Drawing.Size(131, 17);
this.tsslInfo.Text = "toolStripStatusLabel1";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(469, 336);
this.Controls.Add(this.tabControl1);
this.Controls.Add(this.statusStrip1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "進(jìn)程管理器";
this.Load += new System.EventHandler(this.Form1_Load);
this.contextMenuStrip1.ResumeLayout(false);
this.tabControl1.ResumeLayout(false);
this.tabPage1.ResumeLayout(false);
this.statusStrip1.ResumeLayout(false);
this.statusStrip1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
private System.Windows.Forms.ToolStripMenuItem 刷新ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 結(jié)束進(jìn)程ToolStripMenuItem;
private System.Windows.Forms.TabControl tabControl1;
private System.Windows.Forms.TabPage tabPage1;
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.ColumnHeader columnHeader11;
private System.Windows.Forms.ColumnHeader columnHeader12;
private System.Windows.Forms.ColumnHeader columnHeader13;
private System.Windows.Forms.ColumnHeader columnHeader14;
private System.Windows.Forms.ColumnHeader columnHeader15;
private System.Windows.Forms.ColumnHeader columnHeader16;
private System.Windows.Forms.ToolStripMenuItem 設(shè)置優(yōu)先級(jí)ToolStripMenuItem;
private System.Windows.Forms.StatusStrip statusStrip1;
private System.Windows.Forms.ToolStripStatusLabel tsslInfo;
private System.Windows.Forms.ToolStripMenuItem 實(shí)時(shí)ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 高ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 高于標(biāo)準(zhǔn)ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 標(biāo)準(zhǔn)ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 低于標(biāo)準(zhǔn)ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 低ToolStripMenuItem;
}
到此這篇關(guān)于利用C#實(shí)現(xiàn)進(jìn)程管理器的文章就介紹到這了,更多相關(guān)C#進(jìn)程管理器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#編程實(shí)現(xiàn)統(tǒng)計(jì)文件夾內(nèi)文件和隱藏文件的方法示例
這篇文章主要介紹了C#編程實(shí)現(xiàn)統(tǒng)計(jì)文件夾內(nèi)文件和隱藏文件的方法,結(jié)合具體實(shí)例形式分析了C#針對(duì)文件與目錄的遍歷及屬性操作相關(guān)技巧,需要的朋友可以參考下2017-07-07
C#實(shí)現(xiàn)xml文件的讀取與寫入簡單實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)xml文件的讀取與寫入方法,涉及C#操作XML文件的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
基于C#編寫一個(gè)接受圖片流的OCR識(shí)別接口
這篇文章主要為大家詳細(xì)介紹了如何使用C#寫一個(gè)接受圖片流的OCR識(shí)別接口,以及測(cè)試用例調(diào)用接口,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03
C#檢測(cè)移動(dòng)硬盤并獲取移動(dòng)硬盤盤符的方法
這篇文章主要介紹了利用C#檢測(cè)移動(dòng)硬盤并獲取移動(dòng)硬盤盤符2017-12-12
使用Spire.Barcode程序庫生成二維碼的實(shí)例解析
這篇文章主要介紹了使用Spire.Barcode程序庫生成二維碼的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-12-12

