2012-03-13 16 views
93

Tengo una referencia de archivo exe en mi proyecto C#. ¿Cómo invoco ese exe de mi código?Ejecutar un exe del código C#

+0

posible duplicado de [No se puede ejecutar .exe aplicación utilizando código C#] (http://stackoverflow.com/questions/2550434/unable-to-run-exe-application- using-c-sharp-code) –

Respuesta

192
using System.Diagnostics; 

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

Si la aplicación necesita argumentos cmd, usar algo como esto:

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

Ejemplo:

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

Compilar el código

Copie el código y péguelo en el Principal método de una aplicación de consola. Reemplace "mspaint.exe" con la ruta a la aplicación que desea ejecutar.

+8

¿Cómo proporciona esto más valor que las respuestas ya creadas? La respuesta aceptada también muestra el uso de 'Process.Start()' – Default

3

Ejemplo:

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