• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
悠
閒

博客园    首页       联系   管理    订阅  订阅
使用ASP.NET结束进程
【最后更新:2009年8月28日】

一个在ASP.NET页面中结束进程的例子:


前台代码:

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

posted on 2009-08-28 22:02  悠閒  阅读(2650)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3