227

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

0

4 Answers 4

374
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.
        }
    }
}
2
  • 3
    startInfo.UseShellExecute = false was an awesome thing...It worked for me like a charm! Thank you! :)
    – RisingHerc
    Sep 26, 2019 at 7:59
  • @logganB.lehman process hangs forever on exeProcess.WaitForExit(); any idea?
    – Dragon
    Jun 6, 2020 at 13:51
20

Look at Process.Start and Process.StartInfo

1
  • Can you make your answer more comprehensive? And/or point to a duplicate? Aug 31, 2021 at 13:22
19

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.

3
  • 16
    How does this provide more value than the already created answers? The accepted answer also shows the usage of Process.Start()
    – default
    Dec 18, 2014 at 23:23
  • 5
    SO - it's OK to help a beginner with simplified, step by step examples with many details stripped away. Also ok to use caps :P Oct 8, 2018 at 17:54
  • 2
    I just needed a quick way to execute the exe and this was really helpful. Thank you :) Aug 1, 2019 at 9:45
10

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.