Launching an application (.EXE) from C#?

405,196

Solution 1

Use System.Diagnostics.Process.Start() method.

Check out this article on how to use it.

Process.Start("notepad", "readme.txt");

string winpath = Environment.GetEnvironmentVariable("windir");
string path = System.IO.Path.GetDirectoryName(
              System.Windows.Forms.Application.ExecutablePath);

Process.Start(winpath + @"\Microsoft.NET\Framework\v1.0.3705\Installutil.exe",
path + "\\MyService.exe");

Solution 2

Here's a snippet of helpful code:

using System.Diagnostics;

// Prepare the process to run
ProcessStartInfo start = new ProcessStartInfo();
// Enter in the command line arguments, everything you would enter after the executable name itself
start.Arguments = arguments; 
// Enter the executable to run, including the complete path
start.FileName = ExeName;
// Do you want to show a console window?
start.WindowStyle = ProcessWindowStyle.Hidden;
start.CreateNoWindow = true;
int exitCode;


// Run the external process & wait for it to finish
using (Process proc = Process.Start(start))
{
     proc.WaitForExit();

     // Retrieve the app's exit code
     exitCode = proc.ExitCode;
}

There is much more you can do with these objects, you should read the documentation: ProcessStartInfo, Process.

Solution 3

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

Solution 4

System.Diagnostics.Process.Start( @"C:\Windows\System32\Notepad.exe" );

Solution 5

If you have problems using System.Diagnostics like I had, use the following simple code that will work without it:

using System.Diagnostics;

Process notePad = new Process();
notePad.StartInfo.FileName   = "notepad.exe";
notePad.StartInfo.Arguments = "mytextfile.txt";
notePad.Start();
Share:
405,196

Related videos on Youtube

rudigrobler
Author by

rudigrobler

http://wpfdisciples.wordpress.com/bios/rudi-grobler/ http://www.rudigrobler.net/about

Updated on August 22, 2020

Comments

  • rudigrobler
    rudigrobler over 3 years

    How can I launch an application using C#?

    Requirements: Must work on Windows XP and Windows Vista.

    I have seen a sample from DinnerNow.net sampler that only works in Windows Vista.

  • DLeh
    DLeh about 10 years
    Just wanted to point out that this also seems to work with other filetypes than .exes. Just point to the file you want to open and Windows will do its best to open it: System.Diagnostics.Process.Start(@"C:\Users\Blank\Desktop\Pd‌​fFile.pdf");
  • Barton
    Barton about 10 years
    WindowStyle = ProcessWindowStyle.Hidden is for non-GUI. The first time I ran this it failed without UseShellExecute = false, but it works now. Not sure what's going on there...
  • mustaccio
    mustaccio about 8 years
    How does your answer improve on all the previous ones?
  • Paul Sinclair
    Paul Sinclair about 7 years
    How is this "without System.Diagonostics"? Process is in System.Diagnostics.
  • vishal
    vishal over 6 years
    What if i don't know the full name of the exe, i want to call "PathTo*.exe" Is this possible?
  • vishal
    vishal over 6 years
    What if i don't know the full name of the exe, i want to call "PathTo*.exe" Is this possible? Can i use " * " for the rest of the name?
  • sfuqua
    sfuqua over 6 years
    @vishal, this process is for calling a specific executable. You can certainly try using PathTo*.exe but I would not expect it to work. (a) what if there are multiple matches? (b) I would hope Microsoft's code would not allow this, as it would be weak security.
  • KADEM Mohammed
    KADEM Mohammed almost 6 years
    @vishal you need to code a procedure of search to find the executable
  • Amin Mohamed
    Amin Mohamed over 4 years
    Most of people coming to see this post got confused about the path of a file which usually they put in debug folder so when they use my hint "File.exe" directly understand no need a path in this case.