Beispiel #1
0
        public static AsyncProcess Start(ProcessStartInfo startInfo, IDictionary environment)
        {
            startInfo.RedirectStandardError  = true;
            startInfo.RedirectStandardOutput = true;
            startInfo.UseShellExecute        = false;
            var redirecting = true;

            if (AdminPrivilege.IsElevated)
            {
                startInfo.Verb = "";
            }
            else
            {
                if ("runas".EqualsIgnoreCase(startInfo.Verb))
                {
                    redirecting = false;
                    startInfo.UseShellExecute        = true;
                    startInfo.RedirectStandardError  = false;
                    startInfo.RedirectStandardOutput = false;
                }
            }

            if (environment != null)
            {
                foreach (var i in environment.Keys)
                {
                    startInfo.EnvironmentVariables[(string)i] = (string)environment[i];
                }
            }

            var result = new AsyncProcess(new Process {
                StartInfo = startInfo
            });

            result._process.EnableRaisingEvents = true;

            // set up std* access
            if (redirecting)
            {
                result._process.ErrorDataReceived  += result.ErrorDataReceived;
                result._process.OutputDataReceived += result.OutputDataReceived;
            }
            result._process.Exited += result.ProcessExited;
            result._process.Start();

            if (redirecting)
            {
                result._process.BeginErrorReadLine();
                result._process.BeginOutputReadLine();
            }
            return(result);
        }
Beispiel #2
0
        public static AsyncProcess Start(ProcessStartInfo startInfo, IDictionary environment)
        {
            startInfo.RedirectStandardError = true;
            startInfo.RedirectStandardOutput = true;
            startInfo.UseShellExecute = false;
            var redirecting = true;

            if (AdminPrivilege.IsElevated) {
                startInfo.Verb = "";
            } else {
                if ("runas".EqualsIgnoreCase(startInfo.Verb)) {
                    redirecting = false;
                    startInfo.UseShellExecute = true;
                    startInfo.RedirectStandardError = false;
                    startInfo.RedirectStandardOutput = false;
                }
            }

            if (environment != null) {
                foreach (var i in environment.Keys) {
                    startInfo.EnvironmentVariables[(string)i] = (string)environment[i];
                }
            }

            var result = new AsyncProcess(new Process {
                StartInfo = startInfo
            });

            result._process.EnableRaisingEvents = true;

            // set up std* access
            if (redirecting) {
                result._process.ErrorDataReceived += result.ErrorDataReceived;
                result._process.OutputDataReceived += result.OutputDataReceived;
            }
            result._process.Exited += result.ProcessExited;
            result._process.Start();

            if (redirecting) {
                result._process.BeginErrorReadLine();
                result._process.BeginOutputReadLine();
            }
            return result;
        }