Launching an application (.EXE) from C#?

2019-01-01 04:04发布

问题:

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.

回答1:

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

Check out this article on how to use it.



回答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.



回答3:

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


回答4:

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


回答5:

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

Process notePad = new Process();
notePad.StartInfo.FileName   = \"notepad.exe\";
notePad.StartInfo.Arguments = \"mytextfile.txt\";
notePad.Start();


回答6:

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.



回答7:

Adame Kane

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

this worked great!!!!!



回答8:

Try this:

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

(Make sure you use the System.Diagnostics library)



回答9:

Use Process.Start to start a process.

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


回答10:

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

Process.Start(\"File.exe\");