Ejemplo n.º 1
0
            protected virtual void PrepareStart(ProcessArgs arguments, bool silent) {
                _exitedEventPublished = false;
                StandardOutputArray.Clear();
                _standardOutput = null;
                ErrorOutputArray.Clear();
                _errorOutput = null;
                _batchModeOutput = null;
                _batchOutputString = null;
                Killed = false;
                ExitCode = 0;

                _startInfo = new ProcessStartInfo {
                    FileName = ExecutablePath, UseShellExecute = false
                };

                if (arguments != null) {
                    _startInfo.Arguments = arguments.ToCliArgs();
                }

                if (!string.IsNullOrEmpty(WorkingDirectory)) {
                    _startInfo.WorkingDirectory = WorkingDirectory;
                }

                if (silent) {
                    _startInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    _startInfo.CreateNoWindow = true;
                }

                if (RedirectOutput) {
                    _startInfo.RedirectStandardError = true;
                    _startInfo.RedirectStandardOutput = true;
                    if (RedirectedOutputEncoding != null) {
                        _startInfo.StandardErrorEncoding = RedirectedOutputEncoding;
                        _startInfo.StandardOutputEncoding = RedirectedOutputEncoding;
                    }
                }

                _process = new Process {
                    StartInfo = _startInfo
                };

                if (RedirectOutput) {
                    _process.OutputDataReceived += OnProcessOnOutputDataReceived;
                    _process.ErrorDataReceived += OnProcessOnErrorDataReceived;
                }

                if (OnProcessExit != null) {
                    _process.EnableRaisingEvents = true;
                    _process.Exited += ProcessOnExited;
                }
            }
Ejemplo n.º 2
0
        public void Append()
        {
            var args = new ProcessArgs();

            Assert.AreEqual(0, args.Count());
            args.Append("arg 1");
            args.Append((string)null);
            Assert.AreEqual(1, args.Count());
            args.Append(new ProcessArgs().Append("arg2").Append("arg 3"));
            args.Append((ProcessArgs)null);
            Assert.AreEqual(3, args.Count());
            args.Append(4, "arg5", null, "arg6");
            Assert.AreEqual(6, args.Count());
            args.Append(new [] { "ar\"g\"7", "arg8" });
            args.Append((string[])null);
            Assert.AreEqual(8, args.Count());

            Assert.AreEqual("\"arg 1\" arg2 \"arg 3\" 4 arg5 arg6 ar\"\"g\"\"7 arg8", args.ToString());
            Assert.AreEqual("\"arg 1\" arg2 \"arg 3\" 4 arg5 arg6 ar\\\"g\\\"7 arg8", args.ToCliArgs());
        }