2010-11-24 22 views
0

Ich möchte einen Befehl in C# über ein Konsolenbefehlsfenster ausführen. Ich möchte, dass dieser Befehl eine vorhandene exe-Datei mit meiner angegebenen Eingabe ausführt und die Ausgabe in eine andere Datei druckt.Ausführen eines Befehls in C#

Ich habe diesen Code:

System.Diagnostics.Process pProcess = new System.Diagnostics.Process(); 
string l_sCommand = string.Empty; 
l_sCommand += "exe file" + "<" + "Input txt file" + ">" + "output txt file"; 

System.Diagnostics.ProcessStartInfo procStartInfo = 
    new System.Diagnostics.ProcessStartInfo("cmd", "/c " + l_sCommand); 

System.Diagnostics.Process proc = new System.Diagnostics.Process(); 
proc.StartInfo = procStartInfo; 
proc.Start(); 

aber das funktioniert nicht. hat jemand eine Idee warum?

+4

Was genau "funktioniert nicht"? –

+3

Spezifischer sein. Wie funktioniert es nicht? Was passiert stattdessen? – thecoop

+0

Warum behandeln Sie die Umleitung nicht selbst mithilfe von Streams? Oder fragen Sie das? – Rup

Antwort

0

Warum nicht verwenden:

System.Diagnostics.Process.Start("<exe file>", "<args>");

Sie versuchen cmd.exe zu starten? Wenn es sich um eine Befehlszeilen-App handelt, können Sie das EXE einfach über die statische Methode Process.Start starten.

+0

nein, die Eingabedatei simuliert Eingaben vom Benutzer. – Yarden

+0

Ich werde die Frage auf andere Weise stellen. Ich möchte einen Befehl ausführen, als würde ich eine Batch-Datei ausführen - aber ich möchte nicht die Batch-Datei erstellen " – Yarden

+0

Ich sehe. Also, was funktioniert nicht mit Ihrer aktuellen Situation? Ist es fehlerhaft out, oder erstellt es nicht die Ausgabedatei oder etwas anderes? –

0

Verwendung Process.Start Methode übergibt direkt in den Argumenten oder verwenden Sie die eingebaute Funktion Shell

0

Normalerweise übergeben Sie einfach die ausführbaren Dateinamen in der Process.Start-Methode oder die Eigenschaft Process.StartInfo verwenden. Wenn Sie jedoch die Standardausgabe umleiten möchten, müssen Sie Process.StartInfo.RedirectStandardOutput auf true setzen und Process.StandardOutput lesen, wenn der Prozess endet. Siehe unten Beispielcode Lifter von MSDN.

// Start the child process. 
Process p = new Process(); 
// Redirect the output stream of the child process. 
p.StartInfo.UseShellExecute = false; 
p.StartInfo.RedirectStandardOutput = true; 
p.StartInfo.FileName = "Write500Lines.exe"; 
p.Start(); 
// Do not wait for the child process to exit before 
// reading to the end of its redirected stream. 
// p.WaitForExit(); 
// Read the output stream first and then wait. 
string output = p.StandardOutput.ReadToEnd(); 
p.WaitForExit(); 

Weitere Informationen finden Sie in der Dokumentation unter diesem Link. http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput.aspx

0

Ein häufig genug Problem tatsächlich. Die Process-Klasse ist einfach nicht so einfach zu benutzen wie sie sein sollte. (siehe this post) Der folgende Code funktioniert, wenn Sie ProcessRunner class I wrote verwenden.

using CSharpTest.Net.Processes; 
void Exec(string outputFile, Encoding encoding, string program, params string[] args) 
{ 
    using (TextWriter output = new StreamWriter(outputFile, false, encoding)) 
    using (ProcessRunner pi = new ProcessRunner(program)) 
    { 
     pi.OutputReceived += delegate(object sender, ProcessOutputEventArgs e) 
           { output.WriteLine(e.Data); }; 
     pi.Run(args); 
    } 
} 
0

Warum die Ausgabe an einen Puffer nicht streamen und es dann selbst schreiben?

public void CaptureProcess(String Command, String Arguments, String Filename) 
{ 
    // This is the code for the base process 
    Process myProcess = new Process(); 
    myProcess.StartInfo = new ProcessStartInfo(Command, Arguments); 
    myProcess.StartInfo.UseShellExecute = false; 
    myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; 
    myProcess.StartInfo.RedirectStandardOutput = true; 

    // Open a filestream for the output 
    using (FileStream fs = new FileStream(Filename, FileMode.OpenOrCreate)) 
    { 
     using (StreamWriter sw = new StreamWriter(fs)) 
     { 

      // open the stream and capture all output from the process until it dies. 
      using (StreamReader ProcessOutput = myProcess.StandardOutput) 
      { 
       myProcess.Start(); 
       string output = ProcessOutput.ReadToEnd(); 
       sw.Write(output); 

       myProcess.WaitForExit(); 
       myProcess.Close(); 
      } 
     } 
    } 
} 
Verwandte Themen