• C# 实现自动化打开和关闭可执行文件(或 关闭停止与系统交互的可执行文件)


     string file =Application.StartupPath+@"WinFrm_Main.exe";//运行程序位置
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
              //  StopExe(file);
                timer1.Enabled = false; 
              
            }  
            private void button2_Click(object sender, EventArgs e)
            {
                timer1.Enabled = true;
            } 
            void StopExe(string file)
            {
                Process pr = new Process();//声明一个进程类对象
                pr.StartInfo.FileName = file;//指定运行的程序,
                pr.Start();//运行
            }
            void StartExt()
            {
                //Process[] p_arry = Process.GetProcesses();//得到系统所有进程
                //for (int i = 0; i < p_arry.Length; i++)//遍历每个进程
                //{
                //    if (p_arry[i].ProcessName == "WinFrm_Main" && !p_arry[i].Responding)//发现有名为QQ的进程
                //    {
                //        p_arry[i].Kill();//就结束它。
                //        return;
                //    }
                //}
    
                Process[] proc = Process.GetProcessesByName("WinFrm_Main");//创建一个进程数组,把与此进程相关的资源关联。
                for (int i = 0; i < proc.Length; i++)
                {
                    proc[i].Kill();  //逐个结束进程.
                }
    
                System.GC.Collect();//垃圾回收
              
                 
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                Process[] proc = Process.GetProcessesByName("WinFrm_Main");//创建一个进程数组,把与此进程相关的资源关联。
                for (int i = 0; i < proc.Length; i++)
                {
                    if (proc[i].ProcessName == "WinFrm_Main" && !proc[i].Responding)//发现有名为WinFrm_Main的进程 或 WinFrm_Main和电脑停止交互 就退出
                    {
                        proc[i].Kill();//就结束它。 
                        //StopExe(file);
                    }
                }
                if (proc.Length==0)//启动
                    StopExe(file);
                System.GC.Collect();//垃圾回收
    
            }
    

     下载地址:http://download.csdn.net/detail/amy1314250/9849891

  • 相关阅读:
    环保
    A股行情记录
    航运
    黑五类
    家电
    妖股
    高校概念股
    科技园区
    壳股
    白底黑字or黑底白字,眼睛更喜欢哪一个?
  • 原文地址:https://www.cnblogs.com/BensonHai/p/6894293.html
Copyright © 2020-2023  润新知