Beispiel #1
0
        /// <summary>
        /// 执行CMD进程,带参数
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static void RunCMDProcess(string dir, int timeout, delegateNormalInfoReceived shownormal, delegateErrorInfoReceived showerr, params string[] inputs)
        {
            try
            {
                System.Diagnostics.Process p = CreateProcess(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "cmd.exe"), dir);

                p.StartInfo.RedirectStandardError = true;
                bool IsNotLive = true;

                p.ErrorDataReceived += new System.Diagnostics.DataReceivedEventHandler(delegate(object sender, System.Diagnostics.DataReceivedEventArgs e)
                {
                    //ShowErrorInfo(e.Data);
                    if (showerr != null)
                    {
                        showerr(e.Data);
                    }
                    IsNotLive = false;
                });
                p.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(delegate(object sender, System.Diagnostics.DataReceivedEventArgs e)
                {
                    // ShowNormalInfo(e.Data);
                    if (shownormal != null)
                    {
                        shownormal(e.Data);
                    }
                    IsNotLive = false;
                });

                lock (_lockCreateProcess)
                {
                    p.Start();
                }

                p.BeginErrorReadLine();
                p.BeginOutputReadLine();

                if (inputs != null && inputs.Length > 0)//写入执行参数
                {
                    foreach (string input in inputs)
                    {
                        p.StandardInput.WriteLine(input);
                        p.StandardInput.Flush();
                        System.Threading.Thread.Sleep(100);
                    }
                }

                p.StandardInput.WriteLine(" ");
                p.StandardInput.WriteLine("exit");



                while (WaitProcessTimeOut(p, timeout)) //进程超时
                {
                    if (!IsNotLive)                    //只处理上次响应与这次的超时时间
                    {
                        IsNotLive = true;
                        continue;
                    }

                    p.Kill();//关闭进程

                    if (showerr != null)
                    {
                        showerr("ProcessManager.RunCMDProcess 超时 !" + timeout);
                    }

                    return;
                }

                p.WaitForExit();

                p.Close();
                p.Dispose();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #2
0
        /// <summary>
        /// 执行程序
        /// </summary>
        /// <param name="filename"></param>
        public static string RunFileProcess(string filename, string args, string dir, int timeout, bool output, delegateNormalInfoReceived shownormal, delegateErrorInfoReceived showerr)
        {
            System.Diagnostics.Process p = CreateProcess(filename, dir);
            p.StartInfo.Arguments = args;

            StringBuilder result = new StringBuilder();

            p.StartInfo.RedirectStandardError = true;

            p.ErrorDataReceived += new System.Diagnostics.DataReceivedEventHandler(delegate(object sender, System.Diagnostics.DataReceivedEventArgs e)
            {
                if (showerr != null)
                {
                    showerr(e.Data);
                }

                ShowErrorInfo(e.Data);

                result.AppendLine(e.Data);
            });
            p.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(delegate(object sender, System.Diagnostics.DataReceivedEventArgs e)
            {
                if (shownormal != null)
                {
                    shownormal(e.Data);
                }

                ShowNormalInfo(e.Data);
                result.AppendLine(e.Data);
            });

            p.Start();

            if (WaitProcessTimeOut(p, timeout)) //进程超时
            {
                p.Kill();                       //关闭进程
                return("ProcessManager.RunFileProcess 超时 !" + timeout);
            }

            string re = p.StandardOutput.ReadToEnd();

            p.WaitForExit();
            p.Close();
            p.Dispose();
            if (output)
            {
                return(re + result.ToString());
            }

            return("true");
        }