利用C#实现进程管理器

.NET
271
0
0
2023-07-04
标签   C#
目录
  • 实践过程
  • 效果
  • 代码

实践过程

效果

代码

public partial class Form : Form
{
    public Form()
    {
        InitializeComponent();
    }

    private void getProcessInfo()
    {
        try
        {
            listView.Items.Clear();
            Process[] MyProcesses = Process.GetProcesses();
            tsslInfo.Text = "进程总数:" + MyProcesses.Length.ToString();
            string[] Minfo = new string[];
            foreach (Process MyProcess in MyProcesses)
            {
                Minfo[] = MyProcess.ProcessName;
                Minfo[] = MyProcess.MainModule.ModuleName;
                Minfo[] = MyProcess.Threads.Count.ToString();
                Minfo[] = MyProcess.BasePriority.ToString();
                Minfo[] = Convert.ToString(MyProcess.WorkingSet / 1024) + "K";
                Minfo[] = Convert.ToString(MyProcess.VirtualMemorySize / 1024) + "K";
                ListViewItem lvi = new ListViewItem(Minfo, "process");
                listView.Items.Add(lvi);
            }
        }
        catch { }
    }

    private void Form_Load(object sender, EventArgs e)
    {
        getProcessInfo();
    }

    private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        getProcessInfo();
    }

    private void 结束进程ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        try
        {
            if (MessageBox.Show("警告:终止进程会导致不希望发生的结果,\r包括数据丢失和系统不稳定。在被终止前,\r进程将没有机会保存其状态和数据。确实\r想终止该进程吗?", "任务管理器警告", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
            {
                string ProcessName = listView.SelectedItems[0].Text;
                Process[] MyProcess = Process.GetProcessesByName(ProcessName);
                MyProcess[].Kill();
                getProcessInfo();
            }
            else
            { }
        }
        catch
        {
            string ProcessName = listView.SelectedItems[0].Text;
            Process[] MyProcess = Process.GetProcessesByName(ProcessName);
            Process MyProcess = new Process();
            //设定程序名
            MyProcess.StartInfo.FileName = "cmd.exe";
            //关闭Shell的使用
            MyProcess.StartInfo.UseShellExecute = false;
            //重定向标准输入
            MyProcess.StartInfo.RedirectStandardInput = true;
            //重定向标准输出
            MyProcess.StartInfo.RedirectStandardOutput = true;
            //重定向错误输出
            MyProcess.StartInfo.RedirectStandardError = true;
            //设置不显示窗口
            MyProcess.StartInfo.CreateNoWindow = true;
            //执行强制结束命令
            MyProcess.Start();
            MyProcess.StandardInput.WriteLine("ntsd -c q -p " + (MyProcess[0].Id).ToString());
            MyProcess.StandardInput.WriteLine("Exit");
            getProcessInfo();
        }
    }

    private void SetBasePriority(int i)
    {
        string ProcessName = listView.SelectedItems[0].Text;
        Process[] MyProcess = Process.GetProcessesByName(ProcessName);
        switch (i)
        {
            case: MyProcess[0].PriorityClass = ProcessPriorityClass.Idle; break;//低
            case: MyProcess[0].PriorityClass = ProcessPriorityClass.Normal; break;//标准
            case: MyProcess[0].PriorityClass = ProcessPriorityClass.High; break;//高
            case: MyProcess[0].PriorityClass = ProcessPriorityClass.RealTime; break;//实时
            case: MyProcess[0].PriorityClass = ProcessPriorityClass.AboveNormal; break;//高于标准
            case: MyProcess[0].PriorityClass = ProcessPriorityClass.BelowNormal; break;//低于标准
        }
        getProcessInfo();
    }
    private void 实时ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        SetBasePriority();
    }

    private void 高ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        SetBasePriority();
    }

    private void 高于标准ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        SetBasePriority();
    }

    private void 标准ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        SetBasePriority();
    }

    private void 低于标准ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        SetBasePriority();
    }

    private void 低ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        SetBasePriority();
    }
}
partial class Form
{
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows 窗体设计器生成的代码

    /// <summary>
    /// 设计器支持所需的方法 - 不要
    /// 使用代码编辑器修改此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.contextMenuStrip = new System.Windows.Forms.ContextMenuStrip(this.components);
        this.刷新ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.结束进程ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.设置优先级ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.实时ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.高ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.高于标准ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.标准ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.低于标准ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.低ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.tabControl = new System.Windows.Forms.TabControl();
        this.tabPage = new System.Windows.Forms.TabPage();
        this.listView = new System.Windows.Forms.ListView();
        this.columnHeader = new System.Windows.Forms.ColumnHeader();
        this.columnHeader = new System.Windows.Forms.ColumnHeader();
        this.columnHeader = new System.Windows.Forms.ColumnHeader();
        this.columnHeader = new System.Windows.Forms.ColumnHeader();
        this.columnHeader = new System.Windows.Forms.ColumnHeader();
        this.columnHeader = new System.Windows.Forms.ColumnHeader();
        this.statusStrip = new System.Windows.Forms.StatusStrip();
        this.tsslInfo = new System.Windows.Forms.ToolStripStatusLabel();
        this.contextMenuStrip.SuspendLayout();
        this.tabControl.SuspendLayout();
        this.tabPage.SuspendLayout();
        this.statusStrip.SuspendLayout();
        this.SuspendLayout();
        // 
        // contextMenuStrip
        // 
        this.contextMenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.刷新ToolStripMenuItem,
        this.结束进程ToolStripMenuItem,
        this.设置优先级ToolStripMenuItem});
        this.contextMenuStrip.Name = "contextMenuStrip1";
        this.contextMenuStrip.RenderMode = System.Windows.Forms.ToolStripRenderMode.System;
        this.contextMenuStrip.ShowImageMargin = false;
        this.contextMenuStrip.ShowItemToolTips = false;
        this.contextMenuStrip.Size = new System.Drawing.Size(106, 70);
        // 
        // 刷新ToolStripMenuItem
        // 
        this.刷新ToolStripMenuItem.Name = "刷新ToolStripMenuItem";
        this.刷新ToolStripMenuItem.Size = new System.Drawing.Size(, 22);
        this.刷新ToolStripMenuItem.Text = "刷新";
        this.刷新ToolStripMenuItem.Click += new System.EventHandler(this.刷新ToolStripMenuItem_Click);
        // 
        // 结束进程ToolStripMenuItem
        // 
        this.结束进程ToolStripMenuItem.Name = "结束进程ToolStripMenuItem";
        this.结束进程ToolStripMenuItem.Size = new System.Drawing.Size(, 22);
        this.结束进程ToolStripMenuItem.Text = "结束进程";
        this.结束进程ToolStripMenuItem.Click += new System.EventHandler(this.结束进程ToolStripMenuItem_Click);
        // 
        // 设置优先级ToolStripMenuItem
        // 
        this.设置优先级ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.实时ToolStripMenuItem,
        this.高ToolStripMenuItem,
        this.高于标准ToolStripMenuItem,
        this.标准ToolStripMenuItem,
        this.低于标准ToolStripMenuItem,
        this.低ToolStripMenuItem});
        this.设置优先级ToolStripMenuItem.Name = "设置优先级ToolStripMenuItem";
        this.设置优先级ToolStripMenuItem.Size = new System.Drawing.Size(, 22);
        this.设置优先级ToolStripMenuItem.Text = "设置优先级";
        // 
        // 实时ToolStripMenuItem
        // 
        this.实时ToolStripMenuItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
        this.实时ToolStripMenuItem.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
        this.实时ToolStripMenuItem.ImageScaling = System.Windows.Forms.ToolStripItemImageScaling.None;
        this.实时ToolStripMenuItem.Name = "实时ToolStripMenuItem";
        this.实时ToolStripMenuItem.RightToLeft = System.Windows.Forms.RightToLeft.No;
        this.实时ToolStripMenuItem.Size = new System.Drawing.Size(, 22);
        this.实时ToolStripMenuItem.Text = "实时";
        this.实时ToolStripMenuItem.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
        this.实时ToolStripMenuItem.Click += new System.EventHandler(this.实时ToolStripMenuItem_Click);
        // 
        // 高ToolStripMenuItem
        // 
        this.高ToolStripMenuItem.Name = "高ToolStripMenuItem";
        this.高ToolStripMenuItem.Size = new System.Drawing.Size(, 22);
        this.高ToolStripMenuItem.Text = "高";
        this.高ToolStripMenuItem.Click += new System.EventHandler(this.高ToolStripMenuItem_Click);
        // 
        // 高于标准ToolStripMenuItem
        // 
        this.高于标准ToolStripMenuItem.Name = "高于标准ToolStripMenuItem";
        this.高于标准ToolStripMenuItem.Size = new System.Drawing.Size(, 22);
        this.高于标准ToolStripMenuItem.Text = "高于标准";
        this.高于标准ToolStripMenuItem.Click += new System.EventHandler(this.高于标准ToolStripMenuItem_Click);
        // 
        // 标准ToolStripMenuItem
        // 
        this.标准ToolStripMenuItem.Name = "标准ToolStripMenuItem";
        this.标准ToolStripMenuItem.Size = new System.Drawing.Size(, 22);
        this.标准ToolStripMenuItem.Text = "标准";
        this.标准ToolStripMenuItem.Click += new System.EventHandler(this.标准ToolStripMenuItem_Click);
        // 
        // 低于标准ToolStripMenuItem
        // 
        this.低于标准ToolStripMenuItem.Name = "低于标准ToolStripMenuItem";
        this.低于标准ToolStripMenuItem.Size = new System.Drawing.Size(, 22);
        this.低于标准ToolStripMenuItem.Text = "低于标准";
        this.低于标准ToolStripMenuItem.Click += new System.EventHandler(this.低于标准ToolStripMenuItem_Click);
        // 
        // 低ToolStripMenuItem
        // 
        this.低ToolStripMenuItem.Name = "低ToolStripMenuItem";
        this.低ToolStripMenuItem.Size = new System.Drawing.Size(, 22);
        this.低ToolStripMenuItem.Text = "低";
        this.低ToolStripMenuItem.Click += new System.EventHandler(this.低ToolStripMenuItem_Click);
        // 
        // tabControl
        // 
        this.tabControl.Controls.Add(this.tabPage1);
        this.tabControl.Dock = System.Windows.Forms.DockStyle.Fill;
        this.tabControl.Location = new System.Drawing.Point(0, 0);
        this.tabControl.Name = "tabControl1";
        this.tabControl.SelectedIndex = 0;
        this.tabControl.Size = new System.Drawing.Size(469, 314);
        this.tabControl.TabIndex = 6;
        // 
        // tabPage
        // 
        this.tabPage.Controls.Add(this.listView1);
        this.tabPage.Location = new System.Drawing.Point(4, 21);
        this.tabPage.Name = "tabPage1";
        this.tabPage.Padding = new System.Windows.Forms.Padding(3);
        this.tabPage.Size = new System.Drawing.Size(461, 289);
        this.tabPage.TabIndex = 0;
        this.tabPage.Text = "进程";
        this.tabPage.UseVisualStyleBackColor = true;
        // 
        // listView
        // 
        this.listView.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
        this.columnHeader,
        this.columnHeader,
        this.columnHeader,
        this.columnHeader,
        this.columnHeader,
        this.columnHeader});
        this.listView.ContextMenuStrip = this.contextMenuStrip1;
        this.listView.Dock = System.Windows.Forms.DockStyle.Fill;
        this.listView.FullRowSelect = true;
        this.listView.GridLines = true;
        this.listView.Location = new System.Drawing.Point(3, 3);
        this.listView.Name = "listView1";
        this.listView.Size = new System.Drawing.Size(455, 283);
        this.listView.TabIndex = 0;
        this.listView.UseCompatibleStateImageBehavior = false;
        this.listView.View = System.Windows.Forms.View.Details;
        // 
        // columnHeader
        // 
        this.columnHeader.Text = "映像名称";
        this.columnHeader.Width = 100;
        // 
        // columnHeader
        // 
        this.columnHeader.Text = "进程ID";
        this.columnHeader.Width = 70;
        // 
        // columnHeader
        // 
        this.columnHeader.Text = "线程数";
        this.columnHeader.Width = 70;
        // 
        // columnHeader
        // 
        this.columnHeader.Text = "优先级";
        // 
        // columnHeader
        // 
        this.columnHeader.Text = "物理内存";
        this.columnHeader.Width = 65;
        // 
        // columnHeader
        // 
        this.columnHeader.Text = "虚拟内存";
        this.columnHeader.Width = 85;
        // 
        // statusStrip
        // 
        this.statusStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.tsslInfo});
        this.statusStrip.Location = new System.Drawing.Point(0, 314);
        this.statusStrip.Name = "statusStrip1";
        this.statusStrip.Size = new System.Drawing.Size(469, 22);
        this.statusStrip.TabIndex = 7;
        this.statusStrip.Text = "statusStrip1";
        // 
        // tsslInfo
        // 
        this.tsslInfo.Name = "tsslInfo";
        this.tsslInfo.Size = new System.Drawing.Size(, 17);
        this.tsslInfo.Text = "toolStripStatusLabel";
        // 
        // Form
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(F, 12F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(, 336);
        this.Controls.Add(this.tabControl);
        this.Controls.Add(this.statusStrip);
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
        this.MaximizeBox = false;
        this.MinimizeBox = false;
        this.Name = "Form";
        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        this.Text = "进程管理器";
        this.Load += new System.EventHandler(this.Form_Load);
        this.contextMenuStrip.ResumeLayout(false);
        this.tabControl.ResumeLayout(false);
        this.tabPage.ResumeLayout(false);
        this.statusStrip.ResumeLayout(false);
        this.statusStrip.PerformLayout();
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

    private System.Windows.Forms.ContextMenuStrip contextMenuStrip;
    private System.Windows.Forms.ToolStripMenuItem 刷新ToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem 结束进程ToolStripMenuItem;
    private System.Windows.Forms.TabControl tabControl;
    private System.Windows.Forms.TabPage tabPage;
    private System.Windows.Forms.ListView listView;
    private System.Windows.Forms.ColumnHeader columnHeader;
    private System.Windows.Forms.ColumnHeader columnHeader;
    private System.Windows.Forms.ColumnHeader columnHeader;
    private System.Windows.Forms.ColumnHeader columnHeader;
    private System.Windows.Forms.ColumnHeader columnHeader;
    private System.Windows.Forms.ColumnHeader columnHeader;
    private System.Windows.Forms.ToolStripMenuItem 设置优先级ToolStripMenuItem;
    private System.Windows.Forms.StatusStrip statusStrip;
    private System.Windows.Forms.ToolStripStatusLabel tsslInfo;
    private System.Windows.Forms.ToolStripMenuItem 实时ToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem 高ToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem 高于标准ToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem 标准ToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem 低于标准ToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem 低ToolStripMenuItem;
}