1 Pluspunkt 0 Minuspunkte
Wie kann ich in C# einen Systembefehl wie "ipconfig" ausführen und die Ausgabe des Befehl als String erhalten?
von  

1 Antwort

0 Pluspunkte 0 Minuspunkte

Mit dem System.Diagnostics Namespace.

using (Process process = new Process())
{
    ProcessStartInfo startInfo = new ProcessStartInfo
    {
        FileName = "cmd.exe",
        Arguments = "/c ipconfig",
        RedirectStandardOutput = true,
        RedirectErrorOutput = true,
        UseShellExecute = false,
        CreateNoWindow = true
    };

    process.StartInfo = startInfo;
    process.Start();
    process.WaitForExit();

    using (StreamReader reader = process.StandardOutput)
    {
        return reader.ReadToEnd();
    }
}
von (396 Punkte)