예제 #1
0
        public static AsyncProcess Start(ProcessStartInfo startInfo, IDictionary environment)
        {
            startInfo.RedirectStandardError  = true;
            startInfo.RedirectStandardOutput = true;
            startInfo.UseShellExecute        = false;
            startInfo.CreateNoWindow         = true;
            startInfo.WindowStyle            = ProcessWindowStyle.Hidden;

            bool redirecting = true;

            //if (AdminPrivilege.IsElevated) {
            //startInfo.Verb = "";
            //} else {
            if ("runas".Equals(startInfo.Verb))
            {
                redirecting = false;

                startInfo.Arguments = string.Format("/c {0} {1}", startInfo.FileName, startInfo.Arguments);
                startInfo.FileName  = "cmd.exe";

                startInfo.UseShellExecute        = false;
                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);
        }
예제 #2
0
        public static AsyncProcess Start(ProcessStartInfo startInfo, IDictionary environment)
        {
            startInfo.RedirectStandardError = true;
            startInfo.RedirectStandardOutput = true;
            startInfo.UseShellExecute = false;
            startInfo.CreateNoWindow = true;
            startInfo.WindowStyle = ProcessWindowStyle.Hidden;

            bool redirecting = true;

            //if (AdminPrivilege.IsElevated) {
            //startInfo.Verb = "";
            //} else {
            if ("runas".Equals(startInfo.Verb)) {
                redirecting = false;

                startInfo.Arguments = string.Format("/c {0} {1}", startInfo.FileName, startInfo.Arguments);
                startInfo.FileName = "cmd.exe";

                startInfo.UseShellExecute = false;
                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;
        }