views:

2266

answers:

5

How can I launch a application using C#?

Requirements: MUST work on XP and VISTA?

I have seen a sample from DinnerNow.net sampler that only works in vista?

+13  A: 
System.Diagnostics.Process.Start("PathToExe.exe");
Mark S. Rasmussen
+18  A: 

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

Check out this article on how to use it.

hmemcpy
+2  A: 

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
+9  A: 

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;

// 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.

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