How to run external program via a C#?
To run an external program in C#, we can useProcess
object which we can specify the executable file path using StartInfo.FileName
property and then call Start()
method to start that executable file.Example
In below example we are settingStartInfo.WindowStyle
property to maximized so it will show up to the user, and we are using method WaitForExit()
to make the current thread wait until the associated process terminates. It should be called after all other methods are called on the process.using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
public class Example
{
public static void Main()
{
Process ExternalProcess = new Process();
ExternalProcess.StartInfo.FileName = "Notepad.exe";
ExternalProcess.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
ExternalProcess.Start();
ExternalProcess.WaitForExit();
}
}