2009-11-30 13 views

risposta

9
System.Diagnostics.Process p = new System.Diagnostics.Process(); 
p.StartInfo.FileName = "blah.lua arg1 arg2 arg3"; 
p.StartInfo.UseShellExecute = true; 
p.Start(); 

Un altro modo sarebbe quello di utilizzare P/Invoke e utilizzare ShellExecute direttamente:

[DllImport("shell32.dll")] 
static extern IntPtr ShellExecute(
    IntPtr hwnd, 
    string lpOperation, 
    string lpFile, 
    string lpParameters, 
    string lpDirectory, 
    ShowCommands nShowCmd); 
+0

ho bisogno di execut uno script lua ... – RCIX

+0

@RCIX: come lo hai fatto ora? Il modo manuale intendo. –

+0

, cioè mettendo 'blah.lua somearg anotherarg thirdarg' in un comando della console. – RCIX

2

C'è un modo semplice per gestire questo in C#. Utilizzando lo spazio dei nomi System.Diagnostics, esiste una classe per gestire i processi di spawning.

System.Diagnostics.Process process = new System.Diagnostics.Process(); 
process.StartInfo.FileName = "App.exe"; 
process.StartInfo.Arguments = "arg1 arg2 arg3"; 
process.Start(); 

Console.WriteLine(process.StandardOutput.ReadToEnd(); 

Non ci sono parametri aggiuntivi per gestire le cose come non la creazione di una finestra di console, il reindirizzamento ingresso o uscita, e la maggior parte qualsiasi altra cosa avresti bisogno.

6

Si potrebbe voler considerare un approccio asincrono se lo script richiede un po 'di tempo.

Ecco un codice che consente di reindirizzare l'output standard da acquisire per la visualizzazione su un modulo (WPF, Windows Forms, qualunque sia). Si noti che io sto assumendo che non è necessario input degli utenti, in modo da non creare la finestra della console, che sembra migliore:

BackgroundWorker worker = new BackgroundWorker(); 
... 
// Wire up event in the constructor or wherever is appropriate 
worker.DoWork += new DoWorkEventHandler(worker_DoWork); 
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted); 
... 
// Then to execute your script 
worker.RunWorkerAsync("somearg anotherarg thirdarg"); 

void worker_DoWork(object sender, DoWorkEventArgs e) 
{ 
    StringBuilder result = new StringBuilder(); 
    Process process = new Process(); 
    process.StartInfo.FileName = "blah.lua"; 
    process.StartInfo.Arguments = (string)e.Argument; 
    process.StartInfo.UseShellExecute = false; 
    process.StartInfo.RedirectStandardOutput = true; 
    process.StartInfo.CreateNoWindow = true; 
    process.Start(); 
    result.Append(process.StandardOutput.ReadToEnd()); 
    process.WaitForExit(); 
    e.Result = result.AppendLine().ToString(); 
} 

void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
{ 
    if (e.Result != null) console.Text = e.Result.ToString(); 
    else if (e.Error != null) console.Text = e.Error.ToString(); 
    else if (e.Cancelled) console.Text = "User cancelled process"; 
} 
+0

+1 per utilizzare correttamente l'operatore in background e non bloccare l'intero thread. Esperienza utente molto migliore! – ppumkin

Problemi correlati