在ASP.NET中杀死进程
2024-07-10 12:57:12
供稿:网友
通过asp.net可以对一些无用的进程进行远程杀死,下面的代码先列出正在活动的所有进程,然后进行杀死。需要注意的是:这个文件要放在具有administrator访问权限的虚拟目录下。
以下是c#代码:
<%@ page language="c#" %>
<html>
<head>
<% @ import namespace= "system.diagnostics" %>
<script language="c#" runat="server" debug="true">
void page_load(object sender, eventargs e){
btnkill.attributes.add("onclick", "javascript: return confirm('你真的要杀死这个进程吗?');");
}
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+= "杀死" +procname.selecteditem.text + "失败!";
}
}
public void btnkill_click(object sender, system.eventargs e)
{
killprocess(procname.selecteditem.text);
msg.text= procname.selecteditem.text +" 已经被杀死。";
}
public 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()) {
tempname=thisproc.tostring();
begpos = tempname.indexof("(")+1;
endpos= tempname.indexof(")");
tempname=tempname.substring(begpos,endpos-begpos);
proclist.add(tempname);
}
procname.datasource=proclist;
procname.databind();
}
</script>
</head>
<body>
<basefont face="tahoma" />
<center><h2>asp.net 进程杀死器!</h2><br>
<table cellspacing=2 cellpadding=2 border=0 bgcolor="#ffcc66">
<form id="frmproc" runat="server" method="post">
<tr><td><asp:dropdownlist id="procname" runat="server" /></td><td>
进程名字</td></tr>
<tr><td>
<asp:button id="btnkill" text="杀死进程" runat="server" causesvalidation="false" onclick="btnkill_click" />
</td>
<td><asp:button id="btnshow" text="列出所有进程" runat="server" causesvalidation="false" onclick="btnshow_click" />
</td></tr>
</table>
<center><asp:label id="msg" runat="server"/></center>
</form>
</center>
</body>
</html>