187

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rudigrobler
  • 17,045
  • 12
  • 60
  • 74

9 Answers9

242

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.

Marc.2377
  • 7,807
  • 7
  • 51
  • 95
sfuqua
  • 5,797
  • 1
  • 32
  • 33
  • 8
    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\PdfFile.pdf"); – DLeh Jan 29 '14 at 21:10
  • 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... – Barton Feb 21 '14 at 18:12
  • 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? – vishal Jan 11 '18 at 09:21
  • @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. – sfuqua Jan 11 '18 at 15:24
184

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");
Demodave
  • 6,242
  • 6
  • 43
  • 58
Igal Tabachnik
  • 31,174
  • 15
  • 92
  • 157
63
System.Diagnostics.Process.Start("PathToExe.exe");
Mark S. Rasmussen
  • 34,696
  • 4
  • 39
  • 58
22
System.Diagnostics.Process.Start( @"C:\Windows\System32\Notepad.exe" );
Adam Kane
  • 3,996
  • 7
  • 44
  • 53
18

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();
peter.sva
  • 17
  • 4
NDB
  • 618
  • 1
  • 7
  • 16
8

Additionally you will want to use the Environment Variables for your paths if at all possible: http://en.wikipedia.org/wiki/Environment_variable#Default_Values_on_Microsoft_Windows

E.G.

  • %WINDIR% = Windows Directory
  • %APPDATA% = Application Data - Varies alot between Vista and XP.

There are many more check out the link for a longer list.

Brian Schmitt
  • 6,008
  • 1
  • 24
  • 36
5

Just put your file.exe in the \bin\Debug folder and use:

Process.Start("File.exe");
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Amin Mohamed
  • 640
  • 7
  • 10
  • 4
    How does your answer improve on all the previous ones? – mustaccio Apr 15 '16 at 01:55
  • 1
    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. – Amin Mohamed Sep 06 '19 at 21:35
2

Use Process.Start to start a process.

using System.Diagnostics;
class Program
{
    static void Main()
    {
    //
    // your code
    //
    Process.Start("C:\\process.exe");
    }
} 
Deadlock
  • 4,211
  • 1
  • 20
  • 25
1

Try this:

Process.Start("Location Of File.exe");

(Make sure you use the System.Diagnostics library)