用PROCESS类// Get the current process. Process currentProcess = Process.GetCurrentProcess(); // Get all instances of Notepad running on the local // computer. Process [] localByName = Process.GetProcessesByName("notepad"); // Get all instances of Notepad running on the specifiec // computer. // 1. Using the computer alias (do not precede with "\\"). Process [] remoteByName = Process.GetProcessesByName("notepad", "myComputer");// 2. Using an IP address to specify the machineName parameter. Process [] ipByName = Process.GetProcessesByName("notepad", "169.0.0.0"); // Get all processes running on the local computer. Process [] localAll = Process.GetProcesses(); // Get all processes running on the remote computer. Process [] remoteAll = Process.GetProcesses("myComputer"); // Get a process on the local computer, using the process id. Process localById = Process.GetProcessById(1234); // Get a process on a remote computer, using the process id. Process remoteById = Process.GetProcessById(2345, "myComputer");
Process currentProcess = Process.GetCurrentProcess();
// Get all instances of Notepad running on the local
// computer.
Process [] localByName = Process.GetProcessesByName("notepad");
// Get all instances of Notepad running on the specifiec
// computer.
// 1. Using the computer alias (do not precede with "\\").
Process [] remoteByName = Process.GetProcessesByName("notepad", "myComputer");// 2. Using an IP address to specify the machineName parameter.
Process [] ipByName = Process.GetProcessesByName("notepad", "169.0.0.0");
// Get all processes running on the local computer.
Process [] localAll = Process.GetProcesses();
// Get all processes running on the remote computer.
Process [] remoteAll = Process.GetProcesses("myComputer");
// Get a process on the local computer, using the process id.
Process localById = Process.GetProcessById(1234);
// Get a process on a remote computer, using the process id.
Process remoteById = Process.GetProcessById(2345, "myComputer");
Process.GetProcesses
ProcessName = "???"
pro(h).Kill()
private void KillProcess(string processName)
{
System.Diagnostics.Process myproc= new System.Diagnostics.Process();
//得到所有打开的进程
try{
foreach (Process thisproc in Process.GetProcessesByName(processName)) {
if(!thisproc.CloseMainWindow()){
thisproc.Kill();
}
}
}
catch(Exception Exc)
{
msg.Text+= "杀死" + processName + "失败!";
}
获得当前计算机系统内所有的已启动的进程:System.Diagnostics.Process[] processOnComputer = System.Diagnostics.Process.GetProcesses();
foreach ( System.Diagnostics.Process p in processOnComputer )
{
System.Console.WriteLine(p.ToString());
}
Console.ReadLine();
获得当前计算机系统内某个进程并关闭:
System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName("进程名称");foreach ( System.Diagnostics.Process p in process)
{
p.Kill();
}
<%@ Page language="c#" Codebehind="WebForm5.aspx.cs" AutoEventWireup="false" Inherits="BwOA.File.WebForm5" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm5</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<LINK href="../CSS/Admin_StyleSheet.css" type="text/css" rel="stylesheet">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<TABLE id="Table2" style="Z-INDEX: 102; LEFT: 328px; POSITION: absolute; TOP: 256px" cellSpacing="1"
cellPadding="1" width="300" align="center" bgColor="#ffcc66" border="0">
<TR>
<TD align="center" colSpan="2">ASP.NET 进程杀死器!</TD>
</TR>
<TR>
<TD align="center" style="WIDTH: 148px">
进程名字
<ASP:DropDownList id="procname" runat="server"></ASP:DropDownList></TD>
<TD align="center">
<asp:button id="btnShow" runat="server" Text="列出所有进程" CausesValidation="False"></asp:button></TD>
</TR>
<TR>
<TD align="center" style="WIDTH: 148px; HEIGHT: 10px">
<asp:Label id="Label1" runat="server"></asp:Label></TD>
<TD align="center" style="HEIGHT: 10px">
<asp:button id="btnKill" runat="server" Text="杀死进程" CausesValidation="False"></asp:button></TD>
</TR>
<TR>
<TD align="center" colSpan="2">
<asp:Label id="msg" runat="server"></asp:Label></TD>
</TR>
</TABLE>
</form>
</body>
</HTML>
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Diagnostics;
namespace BwOA.File
{
/// <summary>
/// WebForm5 的摘要说明。
/// </summary>
public class WebForm5 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label msg;
protected System.Web.UI.WebControls.Button btnShow;
protected System.Web.UI.WebControls.Button btnKill;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.DropDownList procname;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
btnKill.Attributes.Add("onclick", "return confirm('你真的要杀死这个进程吗?');");
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnShow.Click += new System.EventHandler(this.btnShow_Click);
this.btnKill.Click += new System.EventHandler(this.btnKill_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void btnKill_Click(object sender, System.EventArgs e)
{
msg.Text="";
KillProcess(procname.SelectedItem.Text);
} private void btnShow_Click(object sender, System.EventArgs e)
{
ArrayList procList =new ArrayList();
string tempName="";
int begpos;
int endpos;
foreach (Process thisProc in System.Diagnostics.Process.GetProcesses())
{
try
{
tempName=thisProc.ToString();
begpos = tempName.IndexOf("(")+1;
endpos= tempName.IndexOf(")");
tempName=tempName.Substring(begpos,endpos-begpos);
procList.Add(tempName);
}
catch
{
};
}
procname.DataSource=procList;
procname.DataBind();
Label1.Text="共有"+procname.Items.Count.ToString()+"进程";
}
private void KillProcess(string processName)
{
System.Diagnostics.Process myproc= new System.Diagnostics.Process();
//得到所有打开的进程
try
{
foreach (Process thisproc in Process.GetProcessesByName(processName))
{
if(!thisproc.CloseMainWindow())
{
thisproc.Kill();
}
}
msg.Text= procname.SelectedItem.Text +" 已经被杀死。";
}
catch (Exception ex)
{
msg.Text = ex.Message.ToString();
}
}
}
}