C# 结束进程

 

代码
 
   
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 " ;
}
}
}

 

转载于:https://www.cnblogs.com/zgz_dpl/archive/2010/11/13/1876690.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值