软件开发:使用ASP.NET结束进程
日期:2012/11/26  发布人:润宇软件  浏览量:25
 

<body>
    <form id="form1" runat="server">
        <table align="center" bgcolor="#CDF2FC">
            <tr>
                <td class="style2" colspan="2" style="text-align: center">
                    使用ASP.NET结束进程</td>
            </tr>
            <tr>
                <td align="right" class="style4">
                    选择进程:</td>
                <td class="style3">
                    <asp:DropDownList ID="procname" runat="server" Height="19px" Width="160px">
                    </asp:DropDownList>
                    <asp:Button ID="btnShow" runat="server" onclick="btnShow_Click" Text="刷新进程" />
                </td>
            </tr>
            <tr>
                <td class="style4">
                    <asp:Button ID="btnKill" runat="server" onclick="btnKill_Click" Text="结束进程" />
                </td>
                <td class="style3">
                    <asp:Label ID="msg" runat="server"></asp:Label>
                </td>
            </tr>
            </table>
    </form>
</body>
</html>

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Diagnostics;

public partial class FinishApplication : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        btnKill.Attributes.Add("onclick", "javascript:return confirm('真的要结束这个进程吗?');");
    }
    //
    protected void btnShow_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);
        }
        procname.DataSource = procList;
        procname.DataBind();
    }

    //结束选中的进程
    protected void btnKill_Click(object sender, EventArgs e)
    {
        KillProcess(procname.SelectedItem.Text);
        msg.Text = "进程" + procname.SelectedItem.Text + "已结束";
    }

    //结束进程函数
    private void KillProcess(string processName)
    {
        Process myproc = new Process();
        //得到所有打开的进程
        try
        {
            foreach (Process thisproc in Process.GetProcessesByName(processName))
            {
                if (!thisproc.CloseMainWindow())
                {
                    thisproc.Kill();
                }
            }
        }
        catch (Exception Exc)
        {
            msg.Text += "结束" + procname.SelectedItem.Text + "失败!";
        }
    }
}