ASP.NET中編程殺死進程

來源:互聯網
上載者:User
通過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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.