ASP.NET Process Killer Revisted [C# -BETA 2]

By Peter A. Bromberg, Ph.D.

Peter Bromberg  

Not long ago, I posted a Code Snippet here to show how to use ASP.NET to kill a running process on a remote webserver.



It turns out that System.Diagnostics is uniquely suited to handle stuff like this. In fact, I found it so useful that I've revised the original code to bring all the running processes into an ASP:DropDownList control and DataBind the control from the ArrayList that the processes were stored in. The resulting page looks like this:

<%@ Page Language="c#" %>
<HTML>
<HEAD>
<% @ Import namespace= "System.Diagnostics" %>
<script language="C#" runat="Server" >
void Page_Load(Object Sender, EventArgs e){
btnKill.Attributes.Add("onclick", "javascript: return confirm('Are you sure you want to KILL this process?');");
}

private void KillProcess(string processName){
System.Diagnostics.Process myproc= new System.Diagnostics.Process();
//Get all instances of proc that are open, attempt to close them.
try{
foreach (Process thisproc in Process.GetProcessesByName(processName)) {
if(!thisproc.CloseMainWindow()){
//If closing is not successful or no desktop window handle, then force termination.
thisproc.Kill();
}
} // next proc
}
catch(Exception Exc)
{
msg.Text+= "Attempt to kill " +procname.SelectedItem.Text + " Failed. ";
}
}
public void btnKill_Click(object sender, System.EventArgs e)
{
KillProcess(procname.SelectedItem.Text);
msg.Text= procname.SelectedItem.Text +" is dead, Baby!";
}


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 PROCESS KILLER!</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>
Process Name to Kill</TD></TR>
<TR><TD>
<asp:button id="btnKill" Text="Kill Process" runat="server"
CausesValidation="False" onclick="btnKill_Click" />
</TD>
<TD><asp:button id="btnShow" Text="Show Processes"
runat="server" CausesValidation="False" onclick="btnShow_Click"
/>
</TD></TR>
</TABLE>
<center><asp:Label id="msg" runat="server"/></center>
</form>
</center>
</body>
</HTML>

Let's take a stroll through the code above. First, I import the System.Diagnostics namespace. Then in my Page_Load I'm adding an onclick event handler for the Kill button (btnKill). This is just to provide an "Are you sure" dialog to help the user to avoid making a booboo that they will regret later!

Next comes my KillProcess method that actually iterates through GetProcessesByName collection ( foreach (Process thisproc in Process.GetProcessesByName(processName)) )attempting to match the passed - in process name selected by the user, and first calling the CloseMainWindow() method (if there's a visible interface, like "Notepad") and then, failing that, force termination with thisproc.Kill();. The btnKill_Click handler is where we call KillProcess() and then set the msg.Text in the asp:Label "msg" control to the correct message.

Finally we come to the btnShow_Click handler which is what populates and DataBinds our asp:DropDownList control, "procname". What I'm doing here is creating a new ArrayList, "procList" to hold the list of process names to DataBind to the control. Since the foreach (Process thisProc in System.Diagnostics.Process.GetProcesses()) iteration actually returns a long string in the form of  "SystemDiagnostics.Process (notepad)" , I'm doing some string manipulation to "peel off" just the process name before stuffing it into the ArrayList. (I know, there's a way to do it when you iterate the Process List, but I just didn't feel like looking it up). Finally, with a complete ArrayList, I'm setting the procname.DataSource=procList; and calling procname.DataBind().My control fills with the list of processes for the user. The rest is just HTML setting up my asp.net server controls for the page. Here are a couple of screen clips of the warning alert in action, and what is shown after the process is killed:

 

Of course, you ONLY want to put a page like this in an IIS directory that is specifically marked for Administrator authenticated access. The full ASPX page is available to download at the link below.

Download the code that accompanies this article.


Peter Bromberg is an independent consultant specializing in distributed .NET solutionsa Senior Programmer /Analyst at in Orlando and a co-developer of the NullSkull.com developer website. He can be reached at info@eggheadcafe.com