阅读263 返回首页    go 技术社区[云栖]


ASP.NET中编程杀死进程

通过ASP.NET可以对一些无用的进程进行远程杀死,下面的代码先列出正在活动的所有进程,然后进行杀死。需要注意的是:这个文件要放在具有Administrator访问权限的虚拟目录下。

以下是C#代码:

<%@PageLanguage="c#"%>
<HTML>
<HEAD>
<%@Importnamespace="System.Diagnostics"%>
<scriptlanguage="C#"runat="Server"debug="true">
voidPage_Load(ObjectSender,EventArgse){
btnKill.Attributes.Add("onclick","javascript:returnconfirm('你真的要杀死这个进程吗?');");
}

privatevoidKillProcess(stringprocessName){
System.Diagnostics.Processmyproc=newSystem.Diagnostics.Process();
//得到所有打开的进程
try{
foreach(ProcessthisprocinProcess.GetProcessesByName(processName)){
if(!thisproc.CloseMainWindow()){
thisproc.Kill();
}
}
}
catch(ExceptionExc)
{
msg.Text+="杀死"+procname.SelectedItem.Text+"失败!";
}
}
publicvoidbtnKill_Click(objectsender,System.EventArgse)
{
KillProcess(procname.SelectedItem.Text);
msg.Text=procname.SelectedItem.Text+"已经被杀死。";
}


publicvoidbtnShow_Click(objectsender,System.EventArgse){
ArrayListprocList=newArrayList();
stringtempName="";
intbegpos;
intendpos;
foreach(ProcessthisProcinSystem.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>
<BasefontFace="Tahoma"/>
<center><h2>ASP.NET进程杀死器!</h2><BR>
<Tablecellspacing=2cellpadding=2border=0BGCOLOR="#fFCC66">
<formrunat="Server"method="post">
<TR><TD><ASP:DropDownListrunat="server"/></TD><TD>
进程名字</TD></TR>
<TR><TD>
<asp:buttonText="杀死进程"runat="server"CausesValidation="False"/>
</TD>
<TD><asp:buttonText="列出所有进程"runat="server"CausesValidation="False"/>
</TD></TR>
</TABLE>
<center><asp:Labelrunat="server"/></center>
</form>
</center>
</body>
</HTML>

最后更新:2017-04-02 00:06:33

  上一篇:go ASPX页Web服务调用性能优化
  下一篇:go ASP.NET HTTP运行时组成详解