186

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.

9 Answers 9

243

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.

4
  • 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, 2014 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, 2014 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, 2018 at 9: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, 2018 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");
63
System.Diagnostics.Process.Start("PathToExe.exe");
2
  • What if i don't know the full name of the exe, i want to call "PathTo*.exe" Is this possible?
    – vishal
    Jan 11, 2018 at 9:21
  • @vishal you need to code a procedure of search to find the executable Jun 13, 2018 at 13:37
22
System.Diagnostics.Process.Start( @"C:\Windows\System32\Notepad.exe" );
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();
1
  • 11
    How is this "without System.Diagonostics"? Process is in System.Diagnostics. Feb 15, 2017 at 14:26
9

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.

5

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

Process.Start("File.exe");
2
  • 4
    How does your answer improve on all the previous ones?
    – mustaccio
    Apr 15, 2016 at 1: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. Sep 6, 2019 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");
    }
} 
1

Try this:

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

(Make sure you use the System.Diagnostics library)

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.