风人园

弱水三千,只取一瓢,便能解渴;佛法无边,奉行一法,便能得益。
随笔 - 99, 文章 - 181, 评论 - 56, 引用 - 0
数据加载中……

C#检索当前运行进程,并关闭.

编译工具:Microsoft Visual Studio 2005
使用语言:C#
程序用途:检索当前Windows运行的进程,并可以执行关闭.
详细源码:

代码

using System;

namespace st_Face_GoRush
{
    class Program
    {
        public static void Main(string[] args)
        {
            //开始。
        System.Int16 i;
            System.String StrNameID;
            System.Diagnostics.Process[] ShowID = System.Diagnostics.Process.GetProcesses();
            for (i = 0; i < ShowID.Length; i++)
                System.Console.WriteLine(ShowID[i].ProcessName + " " + ShowID[i].Id);
            System.Threading.Thread.Sleep(200);
            System.Console.Write("请输入你要关闭的进程名称: ");
            StrNameID = System.Convert.ToString(System.Console.ReadLine());
            System.Diagnostics.Process[] CloseID = System.Diagnostics.Process.GetProcessesByName(StrNameID);
            //
            if (CloseID.Length != 0)
            {
                for (i = 0; i < CloseID.Length; i++)
                {
                    if (CloseID[i].Responding && !CloseID[i].HasExited)
                    {
                        System.Console.WriteLine("指定进程存在而且正在响应中...正在关闭.");
                        CloseID[i].CloseMainWindow();
                        if (!CloseID[i].HasExited)
                        {
                            System.Console.WriteLine("由于特别原因无法关闭进程,现在强制关闭!!!");
                            CloseID[i].Kill();
                        }
                    }
                    else
                    {
                        System.Console.WriteLine("指定进程存在但无法响应...正在强制关闭!");
                        CloseID[i].Kill();
                    }
                }
            }
            else
                System.Console.WriteLine("指定进程不存在无法关闭!请确认输入正确.");
        }

    }
}

posted on 2006-12-22 12:02 风人园 阅读(4031) 评论(0)  编辑  收藏 所属分类: DotNet