代码
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static string str;
public static int count;
public static long size;
private void Form1_Load( object sender, EventArgs e)
{
bind_listview();
}
private void KillProcess( string processName)
{
System.Diagnostics.Process myproc = new System.Diagnostics.Process();
// 得到所有打开的进程
Process[] processes = System.Diagnostics.Process.GetProcesses();
for ( int i = 0 ; i <= processes.Length - 1 ;i ++ )
{
if (processName == processes[i].ProcessName.ToString())
{
try
{
foreach (Process thisproc in Process.GetProcessesByName(processName))
{
if ( ! thisproc.CloseMainWindow())
{
thisproc.Kill();
}
}
MessageBox.Show( " 杀死 " + processName + " 成功! " );
}
catch (Exception e)
{
MessageBox.Show( " 杀死 " + processName + " 失败! " );
}
}
}
}
public void bind_listview()
{
string processnamestr = "" ;
Process[] processes = System.Diagnostics.Process.GetProcesses();
this .listView1.Items.Clear();
for ( int i = 0 ; i <= processes.Length - 1 ; i ++ )
{
if (processnamestr.IndexOf(processes[i].ProcessName.ToString()) <= 0 )
{
this .listView1.Items.Add( " row " + (i + 1 ).ToString(), processes[i].ProcessName.ToString(), 0 );
this .listView1.Items[ " row " + (i + 1 ).ToString()].SubItems.Add( " 1 " );
this .listView1.Items[ " row " + (i + 1 ).ToString()].SubItems.Add(processes[i].PeakWorkingSet64.ToString());
processnamestr += processes[i].ProcessName.ToString() + " , " ;
}
else
{
string [] pname = processnamestr.Split( ' , ' );
for ( int j = 0 ; j <= pname.Length - 1 ; j ++ )
{
if (pname[j] == processes[i].ProcessName.ToString().Trim())
{
// MessageBox.Show(this.listView1.Items[j].SubItems[2].Text.ToString());
int t = Convert.ToInt16( this .listView1.Items[j].SubItems[ 1 ].Text.ToString());
long nl = Convert.ToInt64( this .listView1.Items[j].SubItems[ 2 ].Text) + processes[i].PeakWorkingSet64;
t ++ ;
this .listView1.Items[j].SubItems[ 1 ].Text = t.ToString();
this .listView1.Items[j].SubItems[ 2 ].Text = nl.ToString();
break ;
}
}
}
}
}
private void button1_Click( object sender, EventArgs e)
{
// 结束进程
KillProcess(str);
bind_listview();
}
private void button2_Click( object sender, EventArgs e)
{
bind_listview();
}
private void listView1_SelectedIndexChanged( object sender, EventArgs e)
{
count = 0 ;
size = 0 ;
foreach (ListViewItem lst in listView1.SelectedItems)
{
str = lst.Text;
}
Process[] processes = System.Diagnostics.Process.GetProcesses();
for ( int i = 0 ; i <= processes.Length - 1 ; i ++ )
{
if (str == processes[i].ProcessName.ToString())
{
count ++ ;
size += processes[i].PeakWorkingSet64;
}
}
this .label2.Text = " 系统中运行了 " + count.ToString() + " 个 " + str + " 进程 " ;
this .label3.Text = " 共占用内存 " + size.ToString() + " K " ;
}
}
}
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static string str;
public static int count;
public static long size;
private void Form1_Load( object sender, EventArgs e)
{
bind_listview();
}
private void KillProcess( string processName)
{
System.Diagnostics.Process myproc = new System.Diagnostics.Process();
// 得到所有打开的进程
Process[] processes = System.Diagnostics.Process.GetProcesses();
for ( int i = 0 ; i <= processes.Length - 1 ;i ++ )
{
if (processName == processes[i].ProcessName.ToString())
{
try
{
foreach (Process thisproc in Process.GetProcessesByName(processName))
{
if ( ! thisproc.CloseMainWindow())
{
thisproc.Kill();
}
}
MessageBox.Show( " 杀死 " + processName + " 成功! " );
}
catch (Exception e)
{
MessageBox.Show( " 杀死 " + processName + " 失败! " );
}
}
}
}
public void bind_listview()
{
string processnamestr = "" ;
Process[] processes = System.Diagnostics.Process.GetProcesses();
this .listView1.Items.Clear();
for ( int i = 0 ; i <= processes.Length - 1 ; i ++ )
{
if (processnamestr.IndexOf(processes[i].ProcessName.ToString()) <= 0 )
{
this .listView1.Items.Add( " row " + (i + 1 ).ToString(), processes[i].ProcessName.ToString(), 0 );
this .listView1.Items[ " row " + (i + 1 ).ToString()].SubItems.Add( " 1 " );
this .listView1.Items[ " row " + (i + 1 ).ToString()].SubItems.Add(processes[i].PeakWorkingSet64.ToString());
processnamestr += processes[i].ProcessName.ToString() + " , " ;
}
else
{
string [] pname = processnamestr.Split( ' , ' );
for ( int j = 0 ; j <= pname.Length - 1 ; j ++ )
{
if (pname[j] == processes[i].ProcessName.ToString().Trim())
{
// MessageBox.Show(this.listView1.Items[j].SubItems[2].Text.ToString());
int t = Convert.ToInt16( this .listView1.Items[j].SubItems[ 1 ].Text.ToString());
long nl = Convert.ToInt64( this .listView1.Items[j].SubItems[ 2 ].Text) + processes[i].PeakWorkingSet64;
t ++ ;
this .listView1.Items[j].SubItems[ 1 ].Text = t.ToString();
this .listView1.Items[j].SubItems[ 2 ].Text = nl.ToString();
break ;
}
}
}
}
}
private void button1_Click( object sender, EventArgs e)
{
// 结束进程
KillProcess(str);
bind_listview();
}
private void button2_Click( object sender, EventArgs e)
{
bind_listview();
}
private void listView1_SelectedIndexChanged( object sender, EventArgs e)
{
count = 0 ;
size = 0 ;
foreach (ListViewItem lst in listView1.SelectedItems)
{
str = lst.Text;
}
Process[] processes = System.Diagnostics.Process.GetProcesses();
for ( int i = 0 ; i <= processes.Length - 1 ; i ++ )
{
if (str == processes[i].ProcessName.ToString())
{
count ++ ;
size += processes[i].PeakWorkingSet64;
}
}
this .label2.Text = " 系统中运行了 " + count.ToString() + " 个 " + str + " 进程 " ;
this .label3.Text = " 共占用内存 " + size.ToString() + " K " ;
}
}
}