Wie führe ich ein Befehlszeilenprogramm von C# aus und erhalte die STD OUT Ergebnisse zurück? Insbesondere möchte ich DIFF auf zwei Dateien ausführen, die programmgesteuert ausgewählt werden und die Ergebnisse in ein Textfeld schreiben.
Antworten
Zu viele Anzeigen?Ein einzeiliger Laufbefehl:
new Process() { StartInfo = new ProcessStartInfo("echo", "Hello, World") }.Start();
Lesen Sie die Ausgabe des Befehls in der kürzesten Menge an lesbarem Code:
var cliProcess = new Process() {
StartInfo = new ProcessStartInfo("echo", "Hello, World") {
UseShellExecute = false,
RedirectStandardOutput = true
}
};
cliProcess.Start();
string cliOut = cliProcess.StandardOutput.ReadToEnd();
cliProcess.WaitForExit();
cliProcess.Close();
Falls Sie auch einen Befehl in der cmd.exe ausführen müssen, können Sie wie folgt vorgehen:
// 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 = "cmd.exe";
p.StartInfo.Arguments = "/C vol";
p.Start();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Console.WriteLine(output);
Dies gibt nur die Ausgabe des Befehls selbst zurück:
Sie können auch Folgendes verwenden StandardInput
代わりに StartInfo.Arguments
:
// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "cmd.exe";
p.Start();
// Read the output stream first and then wait.
p.StandardInput.WriteLine("vol");
p.StandardInput.WriteLine("exit");
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Console.WriteLine(output);
Das Ergebnis sieht wie folgt aus:
Dies könnte für jemanden nützlich sein, der versucht, den lokalen ARP-Cache auf einem PC/Server abzufragen.
List<string[]> results = new List<string[]>();
using (Process p = new Process())
{
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.Arguments = "/c arp -a";
p.StartInfo.FileName = @"C:\Windows\System32\cmd.exe";
p.Start();
string line;
while ((line = p.StandardOutput.ReadLine()) != null)
{
if (line != "" && !line.Contains("Interface") && !line.Contains("Physical Address"))
{
var lineArr = line.Trim().Split(' ').Select(n => n).Where(n => !string.IsNullOrEmpty(n)).ToArray();
var arrResult = new string[]
{
lineArr[0],
lineArr[1],
lineArr[2]
};
results.Add(arrResult);
}
}
p.WaitForExit();
}
Sie können ein beliebiges Befehlszeilenprogramm mit der Process-Klasse starten und die Eigenschaft StandardOutput der Process-Instanz mit einem von Ihnen erstellten Stream-Reader (entweder basierend auf einer Zeichenkette oder einem Speicherort) festlegen. Nach Beendigung des Prozesses können Sie dann auf diesem Stream beliebige Eingaben vornehmen.
Es gibt eine ProcessHelper-Klasse in PublicDomain Open-Source-Code, der Sie interessieren könnte.