2009-11-30 6 views

Répondre

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

Une autre façon serait d'utiliser P/Invoke et utiliser ShellExecute directement:

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

Je dois EXECUT un script lua ... – RCIX

+0

@RCIX: Comment l'avez-vous fait maintenant? La manière manuelle que je veux dire. –

+0

, c'est-à-dire mettre blah.lua somearg anotherarg thirdarg dans une commande de console. – RCIX

2

Il est un moyen facile de gérer cela en C#. À l'aide de l'espace de noms System.Diagnostics, il existe une classe pour gérer les processus de génération.

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(); 

Il y a des paramètres supplémentaires pour gérer des choses telles que ne pas créer une fenêtre de la console, la redirection d'entrée ou de sortie, et plus tout ce que vous auriez besoin.

6

Vous pourriez envisager une approche asynchrone si le script prend du temps.

Voici un code qui fait cela redirige la sortie standard à capturer pour l'affichage sur un formulaire (WPF, Windows Forms, peu importe). Notez que je suppose que vous n'avez pas besoin d'entrée de l'utilisateur, il ne crée pas la fenêtre de la console, qui ressemble mieux:

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 pour l'utilisation correcte de l'arrière-plan et ne pas bloquer le fil entier. Meilleure expérience utilisateur! – ppumkin

Questions connexes