SourceControl.ProcessReader.RunExecutable C# (CSharp) Method

RunExecutable() public method

Runs an exe, returns the output in a string. If error happens, prints stderr and returns null.
public RunExecutable ( string exeToRun, bool eatFirstLine, string commandLine ) : string
exeToRun string
eatFirstLine bool
commandLine string Command line to execute.
return string
        public string RunExecutable(string exeToRun, bool eatFirstLine, string commandLine)
        {
            this.eatNextLine = eatFirstLine;
            this.executable = exeToRun;
            this.commandLine = commandLine;
            if ((logLevel & LogOptions.ClientUtility) == LogOptions.ClientUtility)
            {
                System.Console.WriteLine("Commandline: " + this.executable + " " + this.commandLine);
            }

            using (Process client = new Process())
            {
                client.StartInfo.UseShellExecute = false;
                client.StartInfo.RedirectStandardError = true;
                client.StartInfo.RedirectStandardOutput = true;
                client.StartInfo.CreateNoWindow = true;
                client.StartInfo.FileName = exeToRun;

                client.StartInfo.Arguments = commandLine;

                client.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
                client.ErrorDataReceived += new DataReceivedEventHandler(ErrorHandler);

                client.Start();

                client.BeginOutputReadLine();
                client.BeginErrorReadLine();

                client.WaitForExit();

                string stderr = errorStringBuilder.ToString();
            }

            return outputStringBuilder.ToString();
        }

Usage Example

Example #1
0
        /// <summary>
        /// Gets the root directory of the repository, derived from the current directory and svn info
        /// </summary>
        /// <returns></returns>
        public string GetLocalRootDirectory()
        {
            Regex urlRegEx = new Regex(@"^(\s)*URL(\s)*:(\s)*(?<url>(\S)*)(\s)*$",
                RegexOptions.ExplicitCapture);

            ProcessReader procReader = new ProcessReader(LogOptions.None);
            string svnInfo = procReader.RunExecutable(ClientExe, false, "info");
            string url = null;

            if (svnInfo != null)
            {
                StringReader sr = new StringReader(svnInfo);
                while (url == null)
                {
                    string l = sr.ReadLine();
                    if (l == null)
                        break;

                    Match urlMatch = urlRegEx.Match(l);
                    if (urlMatch.Success)
                    {
                        url = urlMatch.Groups[1].Value;
                    }
                }
            }

            // the client is unused in Subversion, so generate our from the computer name
            return GetLocalRootDirectory(url);
        }
All Usage Examples Of SourceControl.ProcessReader::RunExecutable