Gal Ratner
Gal Ratner is a Techie who lives and works in Los Angeles. Follow galratner on Twitter
How to Launch an external process from a .NET program

You can invoke and Launch any program from your .NET code using the operating system shell or using your prgram as a shell. here is how:

   50 public static string RunExternalProcess(string pathToProgram, bool useShellExecute, string args, bool waitForExit, int waitTime, string userName, string password)

   51         {

   52             string result = string.Empty;

   53 

   54             using (Process process = new Process())

   55             {

   56                 ProcessStartInfo processStartInfo = new ProcessStartInfo();

   57 

   58                 try

   59                 {

   60                     processStartInfo.FileName = pathToProgram;

   61                     processStartInfo.Arguments = args;

   62                     processStartInfo.CreateNoWindow = true;

   63                     processStartInfo.UseShellExecute = useShellExecute;

   64                     if (!useShellExecute)

   65                     {

   66                         processStartInfo.RedirectStandardOutput = true;

   67                         processStartInfo.RedirectStandardError = true;

   68                         if (!string.IsNullOrEmpty(userName))

   69                             processStartInfo.UserName = userName;

   70                         if (!string.IsNullOrEmpty(password))

   71                         {

   72                             processStartInfo.Password = new System.Security.SecureString();

   73                             char[] passwordChars = password.ToCharArray();

   74                             foreach (char c in passwordChars)

   75                                 processStartInfo.Password.AppendChar(c);

   76                         }

   77                     }

   78                     process.StartInfo = processStartInfo;

   79                     process.Start();

   80                     if (!useShellExecute)

   81                         result = process.StandardError.ReadToEnd();

   82                     if (useShellExecute && waitForExit)

   83                     {

   84                         if (waitTime == int.MaxValue)

   85                             process.WaitForExit();

   86                         else

   87                             process.WaitForExit(waitTime);

   88                     }

   89                     if (!process.HasExited && !useShellExecute)

   90                         process.Kill();

   91                 }

   92                 catch (Exception e)

   93                 {

   94                     result = e.Message;

   95                 }

   96             }

   97             return result;

   98         }


Using it will look like this: string result = RunExternalProcess("java", false, "-version", true, 3000, string.Empty, string.Empty);

 

Here is the same function. this time using non blocking reads. This will help avoid deadlocks in case the program doesnt close the output or error stream.

public static bool RunExternalProcess(string pathToProgram, string args, bool waitForExit, int waitTime, string userName, string password, bool readError, bool readOutput, out string output, out string error)

        {

            string outputString = string.Empty;

            string errorString = string.Empty;

 

            using (Process process = new Process())

            {

                ProcessStartInfo processStartInfo = new ProcessStartInfo();

                try

                {

                    processStartInfo.FileName = pathToProgram;

                    processStartInfo.Arguments = args;

                    processStartInfo.CreateNoWindow = true;

                    processStartInfo.UseShellExecute = false;

                    if (readOutput)

                    {

                        processStartInfo.RedirectStandardOutput = true;

                        process.OutputDataReceived += (sender, e) => outputString += e.Data;

                    }

                    if (readError)

                    {

                        processStartInfo.RedirectStandardError = true;

                        process.ErrorDataReceived += (sender, e) => errorString += e.Data;

                    }

                    if (!string.IsNullOrEmpty(userName))

                        processStartInfo.UserName = userName;

                    if (!string.IsNullOrEmpty(password))

                    {

                        processStartInfo.Password = new System.Security.SecureString();

                        char[] passwordChars = password.ToCharArray();

                        foreach (char c in passwordChars)

                            processStartInfo.Password.AppendChar(c);

                    }

                    process.StartInfo = processStartInfo;

                    process.Start();

                    if (readOutput)

                        process.BeginOutputReadLine();

                    if (readError)

                        process.BeginErrorReadLine();

                    if (waitForExit)

                        process.WaitForExit(waitTime);

                    if (!process.HasExited)

                        process.Kill();

                    output = outputString;

                    error = errorString;

                    return true;

                }

                catch (Exception e)

                {

                    output = string.Empty;

                    error = e.Message;

                }

            }

            return false;

        }


Posted 26 Apr 2009 2:46 PM by Gal Ratner
Filed under:

Powered by Community Server (Non-Commercial Edition), by Telligent Systems