zoukankan      html  css  js  c++  java
  • 结束explorer进程

                  /// <summary>
            /// 以杀死进程的方式杀死explorer
            /// </summary>
            /// <param name="name">要杀死的进程友好名(无 .exe)</param>
            static public void killProcess(string name)
            {
                try
                {
                    Process[] myProcesses = Process.GetProcesses();
                    foreach (Process myProcess in myProcesses)
                    {
                        if (name == myProcess.ProcessName)
                            myProcess.Kill();
                    }
                }
                catch (Exception ee)
                {
                    //抛出异常
                    MessageBox.Show(ee.Message);
                }
            }
    
            //static public void cmdkill(string name)
            //{
            //    string str = @"taskkill /f /im explorer.exe";
            //    Process p = new Process();                                    //建立外部调用线程
            //    p.StartInfo.FileName = "ffmpeg.exe";                          //要调用外部程序的绝对路径
            //    p.StartInfo.Arguments = str;                                  //参数(这里就是FFMPEG的参数了)
            //    p.StartInfo.UseShellExecute = false;                          //不使用操作系统外壳程序启动线程(一定为FALSE,详细的请看MSDN)
            //    p.StartInfo.RedirectStandardError = true;                     //把外部程序错误输出写到StandardError流中(这个一定要注意,FFMPEG的所有输出信息,都为错误输出流,用StandardOutput是捕获不到任何消息的...mencoder就是用standardOutput来捕获的)
            //    p.StartInfo.CreateNoWindow = true;                            //不创建进程窗口
            //    p.ErrorDataReceived += new DataReceivedEventHandler(Output);  //外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
            //    p.Start();                                                    //启动线程
            //    p.BeginErrorReadLine();                                       //开始异步读取
            //    p.WaitForExit();                                              //阻塞等待进程结束
            //    p.Close();                                                    //关闭进程
            //    p.Dispose();                                                  //释放资源
            //    //return true;
            //}
            #region 第一种运行CMD的方式
            /// <summary>
            /// 调用CMD命令来杀死或重启进程
            /// </summary>
            /// <param name="a">杀死或重启进程</param>
            /// <returns>cmd命令返回</returns>
            public static string cmdkill(bool a)
            {
                string str;
                //string str = Console.ReadLine();
                if (a)
                {
                    str = @"taskkill /f /im explorer.exe";
                }
                else
                {
                    //str = @"C:Windowsexplorer.exe";
                    str = @"explorer.exe";
                }
                Process p = new Process();
                p.StartInfo.FileName = "cmd";
                p.StartInfo.UseShellExecute = false;                 //是否使用操作系统shell启动
                p.StartInfo.RedirectStandardInput = true;            //接受来自调用程序的输入信息
                p.StartInfo.RedirectStandardOutput = true;           //由调用程序获取输出信息
                p.StartInfo.RedirectStandardError = true;            //重定向标准错误输出
                p.StartInfo.CreateNoWindow = true;                   //不显示程序窗口
                p.Start();                                           //启动程序
                //向cmd窗口发送输入信息
                p.StandardInput.WriteLine(str + "&exit");
                //p.StandardInput.WriteLine(str);
                p.StandardInput.AutoFlush = true;
                //p.StandardInput.WriteLine("exit");
                //向标准输入写入要执行的命令。这里使用&是批处理命令的符号,表示前面一个命令不管是否执行成功都执行后面(exit)命令,如果不执行exit命令,后面调用ReadToEnd()方法会假死
                //同类的符号还有&&和||前者表示必须前一个命令执行成功才会执行后面的命令,后者表示必须前一个命令执行失败才会执行后面的命令
    
                //获取cmd窗口的输出信息
                string output = p.StandardOutput.ReadToEnd();
                p.WaitForExit();//等待程序执行完退出进程
                p.Close();
                return output;
            }
            #endregion
    
            #region 第二种运行CMD的方式
            /// <summary>
            /// 运行cmd命令
            /// 会显示命令窗口
            /// </summary>
            /// <param name="cmdExe">指定应用程序的完整路径</param>
            /// <param name="cmdStr">执行命令行参数</param>
            static bool RunCmd(string cmdExe, string cmdStr)
            {
                bool result = false;
                try
                {
                    using (Process myPro = new Process())
                    {
                        //指定启动进程是调用的应用程序和命令行参数
                        ProcessStartInfo psi = new ProcessStartInfo(cmdExe, cmdStr);
                        myPro.StartInfo = psi;
                        myPro.Start();
                        myPro.WaitForExit();
                        result = true;
                    }
                }
                catch
                {
                }
                return result;
            }
            /// <summary>
            /// 运行cmd命令
            /// 不显示命令窗口
            /// </summary>
            /// <param name="cmdExe">指定应用程序的完整路径</param>
            /// <param name="cmdStr">执行命令行参数</param>
            static bool RunCmd2(string cmdExe, string cmdStr)
            {
                bool result = false;
                try
                {
                    using (Process myPro = new Process())
                    {
                        myPro.StartInfo.FileName = "cmd.exe";
                        myPro.StartInfo.UseShellExecute = false;
                        myPro.StartInfo.RedirectStandardInput = true;
                        myPro.StartInfo.RedirectStandardOutput = true;
                        myPro.StartInfo.RedirectStandardError = true;
                        myPro.StartInfo.CreateNoWindow = true;
                        myPro.Start();
                        //如果调用程序路径中有空格时,cmd命令执行失败,可以用双引号括起来 ,在这里两个引号表示一个引号(转义)
                        string str = string.Format(@"""{0}"" {1} {2}", cmdExe, cmdStr, "&exit");
                        myPro.StandardInput.WriteLine(str);
                        myPro.StandardInput.AutoFlush = true;
                        myPro.WaitForExit();
                        result = true;
                    }
                }
                catch
                {
                }
                return result;
            }
            #endregion
  • 相关阅读:
    国内外手机号码正则表达式
    apt安装Neo4j
    经典决策树模型
    自动文档摘要评价方法
    scrapy-splash解析javascript
    ubuntu安装splash
    iptables的删除命令中的相关问题
    ARTS第七周
    ARTS第六周
    ARTS第五周
  • 原文地址:https://www.cnblogs.com/zty1294625258/p/9598183.html
Copyright © 2011-2022 走看看