Launching an application (.EXE) from C#?

C#.NetWindows VistaWindows Xp

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

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 2 - C#

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 3 - C#

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

Solution 4 - C#

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

Solution 5 - C#

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();

Solution 6 - C#

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.

Solution 7 - C#

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

Process.Start("File.exe");

Solution 8 - C#

Use Process.Start to start a process.

using System.Diagnostics;
class Program
{
    static void Main()
    {
    //
    // your code
    //
    Process.Start("C:\\process.exe");
    }
} 

Solution 9 - C#

Try this:

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

(Make sure you use the System.Diagnostics library)

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
QuestionrudigroblerView Question on Stackoverflow
Solution 1 - C#sfuquaView Answer on Stackoverflow
Solution 2 - C#Igal TabachnikView Answer on Stackoverflow
Solution 3 - C#Mark S. RasmussenView Answer on Stackoverflow
Solution 4 - C#Adam KaneView Answer on Stackoverflow
Solution 5 - C#NDBView Answer on Stackoverflow
Solution 6 - C#Brian SchmittView Answer on Stackoverflow
Solution 7 - C#Amin MohamedView Answer on Stackoverflow
Solution 8 - C#DeadlockView Answer on Stackoverflow
Solution 9 - C#user6436606View Answer on Stackoverflow