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

<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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值