Monday 23 June 2008

Executing external applications

Some interesting information about setting up an external application (command line) and executing it without a visible window. Allows for capturing the stdout, stdin and stderr streams too. This example is for launching the command line svn client with info argument about a specific file in the svn repository, and capturing the result of our query.

   1:  Process svnProcess = new Process();
   2:  svnProcess.StartInfo.FileName = "svn.exe";
   3:  svnProcess.StartInfo.Arguments = " info " + svnUri;
   4:  svnProcess.StartInfo.UseShellExecute = false;
   5:  svnProcess.StartInfo.RedirectStandardOutput = true;
   6:  svnProcess.StartInfo.RedirectStandardError = true;
   7:  svnProcess.StartInfo.RedirectStandardInput = true;
   8:  svnProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
   9:  svnProcess.StartInfo.CreateNoWindow = true;
  10:   
  11:  try
  12:  {
  13:      bool started = svnProcess.Start();
  14:      if (!started)
  15:      {
  16:          Log.WriteLog("Unable to start svn.exe process, aborting.", "svnInfoError.log", true, true);
  17:          MessageBox.Show("Unable to start svn.exe process.", "Error launching svn.exe", MessageBoxButtons.OK, MessageBoxIcon.Error);
  18:          return -1;
  19:      }
  20:  }
  21:  catch (Exception ex)
  22:  {
  23:      Log.WriteLog("Unable to start svn.exe process, aborting.", "svnInfoError.log", true, true);
  24:      MessageBox.Show("Unable to start svn.exe process.", "Error launching svn.exe", MessageBoxButtons.OK, MessageBoxIcon.Error);
  25:      return -1;
  26:  }
  27:   
  28:  StreamReader sOut = svnProcess.StandardOutput;
  29:  string output;
  30:   
  31:  while (svnProcess.HasExited == false)
  32:  {
  33:      output = sOut.ReadLine();
  34:      if (output != null)
  35:      {
  36:          if (output.StartsWith("Last Changed Rev: "))
  37:          {
  38:              string revNumberString = output.Substring("Last Changed Rev: ".Length);
  39:              int revNumber;
  40:              if (int.TryParse(revNumberString, out revNumber))
  41:              {
  42:                  sOut.Close();
  43:                  return revNumber;
  44:              }
  45:              else
  46:              {
  47:                  throw new Exception("Could not parse the svn revision number!!!");
  48:              }
  49:          }
  50:      }
  51:  }
  52:   
  53:  do
  54:  {
  55:      output = sOut.ReadLine();
  56:      if (output != null)
  57:      {
  58:          if (output.StartsWith("Last Changed Rev: "))
  59:          {
  60:              string revNumberString = output.Substring("Last Changed Rev: ".Length);
  61:              int revNumber;
  62:              if (int.TryParse(revNumberString, out revNumber))
  63:              {
  64:                  sOut.Close();
  65:                  return revNumber;
  66:              }
  67:              else
  68:              {
  69:                  throw new Exception("Could not parse the svn revision number!!!");
  70:              }
  71:          }
  72:      }
  73:  } while (output != null);
  74:   
  75:   
  76:  string error = svnProcess.StandardError.ReadToEnd();
  77:  if (string.IsNullOrEmpty(error) == false)
  78:  {
  79:      Log.WriteLog(error, "svnInfoError.log", true, true);
  80:   
  81:  }
  82:  return -1; // Error executing the svn client application!!!

No comments: