这是我用C# 写的显示本机进程的代码 代码生成没有错误  但就是显示出内容为空 没有显示出进程namespace lianxi
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            dataGridView1.AutoGenerateColumns = false;
        }        private void Form1_Load(object sender, EventArgs e)
        {
            GetAllRunningProcess();
        }        private void GetAllRunningProcess()
        {
            Process[] list = Process.GetProcesses();
            DataTable row = new DataTable();
            row.Columns.Add("Id", typeof(string));
            row.Columns.Add("ProcessName", typeof(string));
            row.Columns.Add("PhysicalMemory", typeof(string));
            row.Columns.Add("StartTime", typeof(string));
            row.Columns.Add("FileName", typeof(string));
            foreach (Process process in list)
            {
                DataRow drow = row.NewRow();
                drow[0] = process.Id.ToString();
                drow[1] = process.ProcessName;
                drow[2] = (process.WorkingSet64 / 1024.0 / 1024).ToString() + "MB";
                try
                {
                    drow[3] = process.StartTime.ToString();
                    drow[4] = process.MainModule.FileName;
                }                catch(System.Exception e)
                {
                    drow[3] = "";
                    drow[4] = "";
                }
                row.Rows.Add(drow);
            }
            dataGridView1.DataSource = row;
        }        private void btnStart_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBoxProcessName.Text))
            {
                MessageBox.Show("启动的进程名不能为空!");
                return;
            }            Process process = new Process();
            ProcessStartInfo startInfo = new ProcessStartInfo(textBoxProcessName.Text.Trim());
            process.StartInfo = startInfo;
            try
            {
                process.Start();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("启动失败!" + ex.Message);
            }
            GetAllRunningProcess();
        }        private void btnStop_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                string temp = dataGridView1.Rows[i].Cells["IsSelect"].FormattedValue.ToString();
                if (temp.Equals("True"))
                {
                    int id = int.Parse(dataGridView1.Rows[i].Cells[i].Value.ToString());
                    Process process = Process.GetProcessById(id);
                    try
                    {
                        process.CloseMainWindow();
                        Thread.Sleep(1000);
                        if (!process.HasExited)
                        {
                            process.Kill();
                        }
                    }                    catch (System.Exception ex)
                    {                    }
                }
            }
            GetAllRunningProcess();
        }
    }
}
多谢了

解决方案 »

  1.   

    F11单步查看数据
    GetAllRunningProcess();执行了显示数据
      

  2.   

    你写的是不是太复杂了
    参考下这个:
    http://haitaowang.blog.163.com/blog/static/1280231192010111112946625/
      

  3.   


    namespace ShowInfo
    {
        partial class Form1
        {
            /// <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.dataGridView1 = new System.Windows.Forms.DataGridView();
                this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
                this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
                this.Column3 = new System.Windows.Forms.DataGridViewTextBoxColumn();
                this.textBox1 = new System.Windows.Forms.TextBox();
                this.button1 = new System.Windows.Forms.Button();
                ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
                this.SuspendLayout();
                // 
                // dataGridView1
                // 
                this.dataGridView1.AllowUserToAddRows = false;
                this.dataGridView1.AllowUserToDeleteRows = false;
                this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
                this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
                this.Column1,
                this.Column2,
                this.Column3});
                this.dataGridView1.Location = new System.Drawing.Point(0, 55);
                this.dataGridView1.Name = "dataGridView1";
                this.dataGridView1.ReadOnly = true;
                this.dataGridView1.RowTemplate.Height = 23;
                this.dataGridView1.Size = new System.Drawing.Size(715, 211);
                this.dataGridView1.TabIndex = 0;
                this.dataGridView1.VirtualMode = true;
                this.dataGridView1.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.dataGridView1_CellValueNeeded);
                // 
                // Column1
                // 
                this.Column1.HeaderText = "Id";
                this.Column1.Name = "Column1";
                this.Column1.ReadOnly = true;
                // 
                // Column2
                // 
                this.Column2.HeaderText = "Name";
                this.Column2.Name = "Column2";
                this.Column2.ReadOnly = true;
                // 
                // Column3
                // 
                this.Column3.HeaderText = "Memory";
                this.Column3.Name = "Column3";
                this.Column3.ReadOnly = true;
                // 
                // textBox1
                // 
                this.textBox1.Location = new System.Drawing.Point(30, 12);
                this.textBox1.Name = "textBox1";
                this.textBox1.Size = new System.Drawing.Size(296, 21);
                this.textBox1.TabIndex = 1;
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(349, 10);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(75, 23);
                this.button1.TabIndex = 2;
                this.button1.Text = "button1";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click += new System.EventHandler(this.button1_Click);
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(715, 266);
                this.Controls.Add(this.button1);
                this.Controls.Add(this.textBox1);
                this.Controls.Add(this.dataGridView1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.Load += new System.EventHandler(this.Form1_Load);
                ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
                this.ResumeLayout(false);
                this.PerformLayout();        }        #endregion        private System.Windows.Forms.DataGridView dataGridView1;
            private System.Windows.Forms.DataGridViewTextBoxColumn Column1;
            private System.Windows.Forms.DataGridViewTextBoxColumn Column2;
            private System.Windows.Forms.DataGridViewTextBoxColumn Column3;
            private System.Windows.Forms.TextBox textBox1;
            private System.Windows.Forms.Button button1;
        }
    }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.Runtime;
    using System.Threading;
    using System.Diagnostics;namespace ShowInfo
    {
        public partial class Form1 : Form
        {        private List<Info> datas = null;        
            public Form1()
            {
                InitializeComponent();
            }
            private void BindData()
            {            if (datas == null)
                    datas = new List<Info>();
                else
                    datas.Clear();            Process[] ps = Process.GetProcesses();            foreach (Process item in ps)
                {
                    datas.Add(new Info() { Id = item.Id, Name = item.ProcessName, Memory = string.Format("{0}MB", item.WorkingSet64 / 1024.0 / 1024) });            }            RefUI();
            }        private void RefUI()
            {
                if (InvokeRequired)
                {
                    BeginInvoke(new Action(RefUI));
                }
                else 
                {
                    dataGridView1.Rows.Clear();                dataGridView1.RowCount = datas.Count;
                    Refresh();
                }
            }        private void dataGridView1_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
            {
                if (datas != null && datas.Count > 0 && datas.Count > e.RowIndex)
                {
                    Info item = datas[e.RowIndex];                switch (e.ColumnIndex)
                    {
                        case 0:
                            e.Value = item.Id;
                            break;
                        case 1:
                            e.Value = item.Name;
                            break;
                        case 2:
                            e.Value = item.Memory;
                            break;
                    }
                }
            }        private void Form1_Load(object sender, EventArgs e)
            {
              
                                            BindData();
             
            }        private void button1_Click(object sender, EventArgs e)
            {
                string name = textBox1.Text;            if (string.IsNullOrEmpty(name))
                {
                    MessageBox.Show("不能为空!");
                    return;
                }            Process process = new Process();
                ProcessStartInfo startInfo = new ProcessStartInfo(name);
                process.StartInfo = startInfo;
                try
                {
                    process.Start();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                BindData();
                
            }       
        }    public class Info
        {
            public int Id;        public string Name;        public string Memory;
        }
    }