閱讀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運行時組成詳解