C# Calling Another Program In a C# Program


One C# console program can call another C# console program. The calling program can pass in arguments to the called program. The called program can pass back its Exit code and any of its console output. The program being called will not display any console interface. The calling program can then take the output of the called program and write it to a log file, if it has a log file to write to. In the example code shown below, there is no log file code.

Here is the code for the first application, the calling application.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
using System.Diagnostics;


namespace CallingProgram
{
    class Program
    {
        // This program is an example of how one program can start another program
        // This is the Calling Program that starts another program called programbeinglaunched.exe
        // This calling program passes arguments to the called program
        static void Main(string[] args)
        {
            int intExitCode = 0;
            // When testing, be sure that the compiled programbeinglaunched.exe
            // has been copied to the same directory as this program is being run from.
            WriteLine("This program, named CallingProgram.exe, starts another program called programbeinglaunched.exe. Press any key to start programbeinglaunched.exe...");
            ReadKey();

            Process pCalledApp = new Process();  // using System.Diagnostics
            pCalledApp.StartInfo.FileName = "programbeinglaunched.exe";
            pCalledApp.StartInfo.Arguments = "myArgu1 myArgu2 myArgu3";
            pCalledApp.StartInfo.UseShellExecute = false;
            pCalledApp.StartInfo.RedirectStandardOutput = true;
            pCalledApp.EnableRaisingEvents = true;

            pCalledApp.Start();
            // To avoid deadlocks, always read the output stream first and then wait.
            string output = pCalledApp.StandardOutput.ReadToEnd();
            pCalledApp.WaitForExit();

            intExitCode = pCalledApp.ExitCode;
            WriteLine("This is now the CallingProgram.exe talking from here on.");
            WriteLine("programbeinglaunched.exe has terminated. It has redirected its output to this program.");
            WriteLine("programbeinglaunched.exe exited with exit code: " + pCalledApp.ExitCode + " " + output);

            WriteLine("Press any key to terminate this Calling applcation.");
            ReadKey();
        }
    }
}

Here is the code for the application being called.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
using System.Diagnostics;

namespace ProgramBeingLaunched
{
    class Program
    {
        static void Main(string[] args)
        {
            int intNumberOfArgs;
            // This program is being launched by another C# program
            // This program receives 3 arguments from the program that launched it
            WriteLine("\nHello. This is 'ProgramBeingLaunched' speaking. Checking for arguments...");
            // Test if input arguments were supplied:
            if (args.Length == 0)
            {
                // no arguments
                WriteLine("No arguments passed in.");
            }
            else
            {
                intNumberOfArgs = args.Length;
                WriteLine($"You passed {intNumberOfArgs} arguments.");
                foreach (string strArg in args)
                {
                    WriteLine("You passed the following argument: " + strArg);
                }
            }
            WriteLine("programbeinglaunched.exe is speaking and now ending.");
        }
    }
}

Here is what the output looks like.

CallingCSharpApp