润宇软件
首 页 企业简介 项目案例 软件定制 行业软件 解决方案 企业动态 服务专区 客服中心
业务介绍:西安软件公司、软件开发、软件定制、软件外包
软件 方案 文章
  润宇软件 >> 新闻资讯  >> 解决方案

软件开发:使用ASP.NET结束进程

发布时间:2015/11/26  浏览次数:24次  字体【    】

<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 + "失败!";
        }
    }
}

  关闭本页
西部IT网合作伙伴 合作伙伴
陕西省 | 榆林 | 延安 | 铜川 | 渭南 | 商洛 | 宝鸡 | 汉中 | 安康 | 咸阳
网站首页 | 关于我们 | 售后服务 | 项目合同 | 查看留言 | 在线留言 | 客服中心
© 版权所有:西安润宇软件科技有限公司 
公司地址:西安市丝路国际创意梦工厂4号楼 联系电话:029-87878512 手机:13468700578 联系人:李先生
Copyright ® 2009-2020 RunYusoft.com Inc. All Rights Reserved 
技术支持:西安润宇软件科技有限公司  陕ICP备11000720号