示例#1
0
        /// <summary>
        /// 连续运行模式,支持打开某程序后,持续向其输入命令,直到结束。
        /// </summary>
        /// <param name="exePath"></param>
        /// <param name="args"></param>
        /// <param name="moreArgs"></param>
        /// <returns></returns>
        public static RunResult RunAsContinueMode(string exePath, string args, string[] moreArgs)
        {
            var result = new RunResult();

            try
            {
                using (var p = GetProcess())
                {
                    p.StartInfo.FileName  = exePath;
                    p.StartInfo.Arguments = args;
                    p.Start();

                    //先输出一个换行,以便将程序的第一行输出显示出来。
                    //如adb.exe,假如不调用此函数的话,第一行等待的shell@android:/ $必须等待下一个命令输入才会显示。
                    p.StandardInput.WriteLine();

                    result.OutputString = ReadStandardOutputLine(p);

                    result.MoreOutputString = new Dictionary <int, string>();
                    for (int i = 0; i < moreArgs.Length; i++)
                    {
                        p.StandardInput.WriteLine(moreArgs[i] + '\r');

                        //必须等待一定时间,让程序运行一会儿,马上读取会读出空的值。
                        Thread.Sleep(WaitTime);

                        result.MoreOutputString.Add(i, ReadStandardOutputLine(p));
                    }

                    // Do not wait for the child process to exit before
                    // reading to the end of its redirected stream.
                    // p.WaitForExit();
                    // Read the output stream first and then wait.
                    p.WaitForExit();

                    result.ExitCode = p.ExitCode;
                    result.Success  = true;
                }
            }
            catch (Win32Exception ex)
            {
                result.Success = false;

                //System Error Codes (Windows)
                //http://msdn.microsoft.com/en-us/library/ms681382(v=vs.85).aspx
                result.OutputString = string.Format("{0},{1}", ex.NativeErrorCode, SystemErrorCodes.ToString(ex.NativeErrorCode));
            }
            catch (Exception ex)
            {
                result.Success      = false;
                result.OutputString = ex.ToString();
            }
            return(result);
        }
示例#2
0
        public static RunResult Run(string exePath, string args)
        {
            var result = new RunResult();

            try
            {
                using (var p = GetProcess())
                {
                    p.StartInfo.FileName  = exePath;
                    p.StartInfo.Arguments = args;
                    p.Start();

                    //获取正常信息
                    if (p.StandardOutput.Peek() > -1)
                    {
                        result.OutputString = p.StandardOutput.ReadToEnd();
                    }

                    //获取错误信息
                    if (p.StandardError.Peek() > -1)
                    {
                        result.OutputString = p.StandardError.ReadToEnd();
                    }

                    // Do not wait for the child process to exit before
                    // reading to the end of its redirected stream.
                    // p.WaitForExit();
                    // Read the output stream first and then wait.
                    p.WaitForExit();

                    result.ExitCode = p.ExitCode;
                    result.Success  = true;
                }
            }
            catch (Win32Exception ex)
            {
                result.Success = false;

                //System Error Codes (Windows)
                //http://msdn.microsoft.com/en-us/library/ms681382(v=vs.85).aspx
                result.OutputString = string.Format("{0},{1}", ex.NativeErrorCode, SystemErrorCodes.ToString(ex.NativeErrorCode));
            }
            catch (Exception ex)
            {
                result.Success      = false;
                result.OutputString = ex.ToString();
            }
            return(result);
        }