How can I run an EXE file from my C# code?

C#.Net

C# Problem Overview


I have an EXE file reference in my C# project. How do I invoke that EXE file from my code?

C# Solutions


Solution 1 - C#

using System.Diagnostics;

class Program
{
    static void Main()
    {
	 	Process.Start("C:\\");
    }
}

If your application needs cmd arguments, use something like this:

using System.Diagnostics;

class Program
{
    static void Main()
    {
	    LaunchCommandLineApp();
    }

    /// <summary>
    /// Launch the application with some options set.
    /// </summary>
    static void LaunchCommandLineApp()
    {
	    // For the example
	    const string ex1 = "C:\\";
	    const string ex2 = "C:\\Dir";

	    // Use ProcessStartInfo class
	    ProcessStartInfo startInfo = new ProcessStartInfo();
	    startInfo.CreateNoWindow = false;
	    startInfo.UseShellExecute = false;
	    startInfo.FileName = "dcm2jpg.exe";
	    startInfo.WindowStyle = ProcessWindowStyle.Hidden;
	    startInfo.Arguments = "-f j -o \"" + ex1 + "\" -z 1.0 -s y " + ex2;

	    try
	    {
	        // Start the process with the info we specified.
	        // Call WaitForExit and then the using statement will close.
	        using (Process exeProcess = Process.Start(startInfo))
	        {
		        exeProcess.WaitForExit();
	        }
	    }
	    catch
	    {
	         // Log error.
	    }
    }
}

Solution 2 - C#

Solution 3 - C#

Example:

System.Diagnostics.Process.Start("mspaint.exe");

Compiling the Code

Copy the code and paste it into the Main method of a console application. Replace "mspaint.exe" with the path to the application you want to run.

Solution 4 - C#

Example:

Process process = Process.Start(@"Data\myApp.exe");
int id = process.Id;
Process tempProc = Process.GetProcessById(id);
this.Visible = false;
tempProc.WaitForExit();
this.Visible = true;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhariView Question on Stackoverflow
Solution 1 - C#Logan B. LehmanView Answer on Stackoverflow
Solution 2 - C#Mark HallView Answer on Stackoverflow
Solution 3 - C#miksiiiView Answer on Stackoverflow
Solution 4 - C#HamidView Answer on Stackoverflow