2012-03-13 23 views
93

Mam odwołanie do pliku exe w moim projekcie C#. Jak mogę wywołać ten exe z mojego kodu?Uruchom plik exe z kodu C#

+0

możliwe duplikat [EXE nie można uruchomić aplikację C# kod] (http://stackoverflow.com/questions/2550434/unable-to-run-exe-application- using-c-sharp-code) –

Odpowiedz

192
using System.Diagnostics; 

class Program 
{ 
    static void Main() 
    { 
     Process.Start("C:\\"); 
    } 
} 

Jeśli aplikacja wymaga cmd argumentów użyć czegoś takiego:

using System.Diagnostics; 

class Program 
{ 
    static void Main() 
    { 
     LaunchCommandLineApp(); 
    } 

    /// <summary> 
    /// Launch the legacy application with some options set. 
    /// </summary> 
    static void LaunchCommandLineApp() 
    { 
     // For the example 
     const string ex1 = "C:\\"; 
     const string ex2 = "C:\\Dir"; 

     // Use ProcessStartInfo class 
     ProcessStartInfo startInfo = new ProcessStartInfo(); 
     startInfo.CreateNoWindow = false; 
     startInfo.UseShellExecute = false; 
     startInfo.FileName = "dcm2jpg.exe"; 
     startInfo.WindowStyle = ProcessWindowStyle.Hidden; 
     startInfo.Arguments = "-f j -o \"" + ex1 + "\" -z 1.0 -s y " + ex2; 

     try 
     { 
      // Start the process with the info we specified. 
      // Call WaitForExit and then the using statement will close. 
      using (Process exeProcess = Process.Start(startInfo)) 
      { 
       exeProcess.WaitForExit(); 
      } 
     } 
     catch 
     { 
      // Log error. 
     } 
    } 
} 
1

Przykład:

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

kompilacji kodu

Skopiuj kod i wklej go do metody Main aplikacji konsoli. Zamień "mspaint.exe" na ścieżkę do aplikacji, którą chcesz uruchomić.

+8

W jaki sposób zapewnia większą wartość niż odpowiedzi już utworzone? Przyjęta odpowiedź pokazuje również użycie 'Process.Start()' – Default

3

przykład:

Process process = Process.Start(@"Data\myApp.exe"); 
int id = process.Id; 
Process tempProc = Process.GetProcessById(id); 
this.Visible = false; 
tempProc.WaitForExit(); 
this.Visible = true;