using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;// 命名空间提供特定的类,使您能够与系统进程、事件日志和性能计数器进行交互。namespace 关闭程序的程序
{
    class Program
    {
        static void Main(string[] args)
        {
            /*Process 类提供下列功能:监视整个网络的系统进程以及启动和停止本地系统进程。
             * 除了检索运行进程列表(通过指定计算机、进程名称或进程 ID)或查看有关当前可访问处理器的进程的信息之外,
             * 还可以获取有关进程线程和模块的详细信息,其方法是通过 Process 类本身,以及分别通过与 ProcessThread 和 ProcessModule 类进行交互来获取。
             * 利用 ProcessStartInfo 类,您可以指定用来启动新进程的多种元素,如输入流、输出流、错误流、工作目录以及命令行谓词和参数。
             * 它们使您能够对进程的行为进行细微的控制。其他相关类用于指定窗口样式、进程和线程优先级以及与线程和模块的集合进行交互。
             */
            Process[] iProcess;
            iProcess = Process.GetProcessesByName("Notepad");
            foreach (Process iidir in iProcess)
            {
                iidir.WaitForExit(3000);
                iidir.CloseMainWindow();
            }
        }
    }
}
其它程序为什么就关不了了。只能关记事本.

解决方案 »

  1.   

    iProcess = Process.GetProcessesByName("Notepad");
    这个只检测记事本进程了啊,当然只能关记事本了
      

  2.   

    //测试过,IE可以关
    //郁闷,刚才测试的时候把贴关了-.-#            Process[] iProcess;
                iProcess = Process.GetProcessesByName("iexplore");
                foreach (Process iidir in iProcess)
                {
                    iidir.WaitForExit(100);
                    iidir.CloseMainWindow();
                }
      

  3.   

    哈哈,清洁工兄,我刚才还想要不要开个玩笑,让楼主把explorer进程关了试试,结果你把iexplore给关了,挺有神农氏的精神,PF一下iProcess = Process.GetProcessesByName("explorer");
      

  4.   

    提供两种思路:一种用进程,如上述代码.一种是用API.
    用进程的代码上面大家都给出了,就不再赘述。下面是API的代码以下是函数声明部分(MSDN上可以查到用法):
            [DllImport("user32.dll")]
            public static extern IntPtr FindWindow(string classname, string title);
            [DllImport("user32.dll", EntryPoint="SendMessageA")]
            public static extern int SendMessage (IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);以下是功能部分:
                IntPtr handle = new IntPtr();
                handle = FindWindow(Notepad, null);//按类名关闭窗体可以用VS自带的SPY+查看类名
                if (handle != IntPtr.Zero)
                {
                    
                    SendMessage(handle,WM_CLOSE,IntPtr.Zero,IntPtr.Zero);
                }
                else
                    MessageBox.Show("未找到了");