关闭

asp.net实现在页面中杀死应用程序(进程)

标签: asp.netbuttonserveraspobjectexception
282人阅读 评论(0) 收藏 举报

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="text-align: center">
        <table border="1" style="width: 273px">
            <tr>
                <td colspan="3">
                    使用asp.net杀死进程</td>
            </tr>
            <tr>
                <td style="text-align: left">
                    进程名字:</td>
                <td colspan="2" style="text-align: left">
                    <asp:DropDownList ID="DropDownList1" runat="server" Width="158px">
                    </asp:DropDownList></td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="杀死进程" /></td>
                <td colspan="2">
                    <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="列出所有进程" /></td>
            </tr>
            <tr>
                <td colspan="3">
                    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></td>
            </tr>
        </table>
   
    </div>
    </form>
</body>
</html>

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Diagnostics;
public partial class KillApplication : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Button1.Attributes.Add("onclick", "javascript:return confirm('你真的要杀死这个进程吗?');");
    }
    protected void Button2_Click(object sender, 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);
        }
        this.DropDownList1.DataSource = proclist;
        this.DropDownList1.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        KillProcess(this.DropDownList1.SelectedItem.Text);
        this.Label1.Text = this.DropDownList1.SelectedItem.Text + "已经被杀死。";
    }
    private void KillProcess(string processname)
    {
        System.Diagnostics.Process myproc = new System.Diagnostics.Process();
        try
        {
            foreach (Process thisproc in System.Diagnostics.Process.GetProcessesByName(processname))
            {
                if (!thisproc.CloseMainWindow())
                {
                    thisproc.Kill();
                }
            }
        }
        catch(Exception e)
        {
            this.Label1.Text += "杀死" + this.DropDownList1.SelectedItem.Text + "失败!";
        }
    }
}
 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:6997次
    • 积分:162
    • 等级:
    • 排名:千里之外
    • 原创:9篇
    • 转载:1篇
    • 译文:0篇
    • 评论:1条
    文章分类
    好的网址