Jak: wykonać wiersz poleceń w C#, uzyskać wyniki STD

Jak wykonać program wiersza poleceń z C# i odzyskać wyniki STD? W szczególności chcę wykonać DIFF na dwóch plikach, które są programowo wybrane i zapisać wyniki w polu tekstowym.

Author: Breeze, 2008-10-15

17 answers

// 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 = "YOURBATCHFILE.bat";
 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();

Kod pochodzi z MSDN .

 544
Author: Ray Jezek,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-05-11 18:23:25

Oto krótka Próbka:

//Create process
System.Diagnostics.Process pProcess = new System.Diagnostics.Process();

//strCommand is path and file name of command to run
pProcess.StartInfo.FileName = strCommand;

//strCommandParameters are parameters to pass to program
pProcess.StartInfo.Arguments = strCommandParameters;

pProcess.StartInfo.UseShellExecute = false;

//Set output of program to be written to process output stream
pProcess.StartInfo.RedirectStandardOutput = true;   

//Optional
pProcess.StartInfo.WorkingDirectory = strWorkingDirectory;

//Start the process
pProcess.Start();

//Get program output
string strOutput = pProcess.StandardOutput.ReadToEnd();

//Wait for process to finish
pProcess.WaitForExit();
 150
Author: Jeremy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-10-15 20:39:22

Tam Jeszcze jeden parametr, który uznałem za przydatny, którego używam do wyeliminowania okna procesu

pProcess.StartInfo.CreateNoWindow = true;

Pomaga to całkowicie ukryć czarne okno konsoli przed Użytkownikiem, jeśli tego pragniesz.

 108
Author: Peter Du,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-09-28 18:36:49
// usage
const string ToolFileName = "example.exe";
string output = RunExternalExe(ToolFileName);

public string RunExternalExe(string filename, string arguments = null)
{
    var process = new Process();

    process.StartInfo.FileName = filename;
    if (!string.IsNullOrEmpty(arguments))
    {
        process.StartInfo.Arguments = arguments;
    }

    process.StartInfo.CreateNoWindow = true;
    process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    process.StartInfo.UseShellExecute = false;

    process.StartInfo.RedirectStandardError = true;
    process.StartInfo.RedirectStandardOutput = true;
    var stdOutput = new StringBuilder();
    process.OutputDataReceived += (sender, args) => stdOutput.AppendLine(args.Data); // Use AppendLine rather than Append since args.Data is one line of output, not including the newline character.

    string stdError = null;
    try
    {
        process.Start();
        process.BeginOutputReadLine();
        stdError = process.StandardError.ReadToEnd();
        process.WaitForExit();
    }
    catch (Exception e)
    {
        throw new Exception("OS error while executing " + Format(filename, arguments)+ ": " + e.Message, e);
    }

    if (process.ExitCode == 0)
    {
        return stdOutput.ToString();
    }
    else
    {
        var message = new StringBuilder();

        if (!string.IsNullOrEmpty(stdError))
        {
            message.AppendLine(stdError);
        }

        if (stdOutput.Length != 0)
        {
            message.AppendLine("Std output:");
            message.AppendLine(stdOutput.ToString());
        }

        throw new Exception(Format(filename, arguments) + " finished with exit code = " + process.ExitCode + ": " + message);
    }
}

private string Format(string filename, string arguments)
{
    return "'" + filename + 
        ((string.IsNullOrEmpty(arguments)) ? string.Empty : " " + arguments) +
        "'";
}
 96
Author: Lu55,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-09 22:16:31

Zaakceptowana odpowiedź na tej stronie ma słabość, która jest kłopotliwa w rzadkich sytuacjach. Istnieją dwa uchwyty plików, do których programy zapisują konwencję, stdout i stderr. Jeśli po prostu odczytasz Pojedynczy Uchwyt Pliku, taki jak Odpowiedź z Raya, a uruchamiany program zapisze wystarczającą ilość danych wyjściowych do stderr, wypełni on wyjściowy bufor stderr i zablokuje. Więc twoje dwa procesy są zablokowane. Rozmiar bufora może wynosić 4K. Jest to niezwykle rzadkie w programach krótkotrwałych, ale jeśli masz długo działający program, który wielokrotnie wyprowadza na stderr, w końcu to się stanie. Jest to trudne do debugowania i wyśledzenia.

Jest kilka dobrych sposobów, by sobie z tym poradzić.
  1. Jednym ze sposobów jest wykonanie cmd.exe zamiast programu i użyj argumentu / c do cmd.exe do wywołania programu wraz z argumentem" 2>&1 " do cmd.exe, aby powiedzieć mu, aby połączył stdout i stderr.

            var p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.Arguments = "/c mycmd.exe 2>&1";
    
  2. Innym sposobem jest użycie modelu programowania, który odczytuje oba uchwyty w tym samym czasie.

            var p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.Arguments = @"/c dir \windows";
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardInput = false;
            p.OutputDataReceived += (a, b) => Console.WriteLine(b.Data);
            p.ErrorDataReceived += (a, b) => Console.WriteLine(b.Data);
            p.Start();
            p.BeginErrorReadLine();
            p.BeginOutputReadLine();
            p.WaitForExit();
    
 15
Author: Cameron,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-11-19 22:10:42
 System.Diagnostics.ProcessStartInfo psi =
   new System.Diagnostics.ProcessStartInfo(@"program_to_call.exe");
 psi.RedirectStandardOutput = true;
 psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
 psi.UseShellExecute = false;
 System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi); ////
 System.IO.StreamReader myOutput = proc.StandardOutput;
 proc.WaitForExit(2000);
 if (proc.HasExited)
  {
      string output = myOutput.ReadToEnd();
 }
 12
Author: Jeff Mc,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-12-03 09:30:44

Będziesz musiał użyć ProcessStartInfo z włączoną RedirectStandardOutput - wtedy możesz odczytać strumień wyjściowy. Może być łatwiejsze użycie ">", aby przekierować wyjście do pliku (przez system operacyjny), a następnie po prostu odczytać plik.

[edit: like what Ray did: +1]

 6
Author: Marc Gravell,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-10-15 20:36:14

Jeśli nie masz nic przeciwko wprowadzeniu zależności, CliWrap może Ci to uprościć:

var cli = new Cli("target.exe");
var output = await cli.ExecuteAsync("arguments", "stdin");
var stdout = output.StandardOutput;
 5
Author: Tyrrrz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-29 17:27:03

Możesz uruchomić dowolny program wiersza poleceń przy użyciu klasy Process i ustawić standardową właściwość Output instancji procesu za pomocą utworzonego czytnika strumieniowego (na podstawie łańcucha znaków lub lokalizacji pamięci). Po zakończeniu procesu możesz zrobić wszystko, czego potrzebujesz na tym strumieniu.

 3
Author: Nick,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-10-15 20:37:31

Może to być przydatne dla kogoś, kto próbuje odpytywać lokalną pamięć podręczną ARP na komputerze / serwerze.

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();
        }
 3
Author: Kitson88,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-22 12:25:39

One-liner run command:

new Process() { StartInfo = new ProcessStartInfo("echo", "Hello, World") }.Start();

Odczyt wyjścia polecenia w najkrótszej ilości kodu reable:

    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();
 3
Author: Dan Stevens,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-10-08 13:08:42

To może nie być najlepszy / najłatwiejszy sposób, ale może być opcja:

Gdy wykonasz z kodu, dodaj " > output.txt", a następnie odczytać na wyjściu.plik txt.

 2
Author: Kon,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-10-15 20:35:07

W PublicDomain otwartym kodzie źródłowym znajduje się Klasa Proceshelper, która może Cię zainteresować.

 2
Author: Shaik,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-08-12 13:17:53

W przypadku, gdy trzeba również wykonać jakieś polecenie w cmd.exe, możesz wykonać następujące czynności:

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

To zwraca tylko wynik samego polecenia:

Tutaj wpisz opis obrazka

Możesz również użyć StandardInput zamiast 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);

Wynik wygląda następująco:

Tutaj wpisz opis obrazka

 2
Author: Konard,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-01-30 13:48:25

Ponieważ większość odpowiedzi tutaj nie implementuje using statemant dla IDisposable i kilka innych rzeczy, które myślę, że mogą być nessecary dodam tę odpowiedź.

Dla C # 8.0

// Start a process with the filename or path with filename e.g. "cmd". Please note the 
//using statemant
using myProcess.StartInfo.FileName = "cmd";
// add the arguments - Note add "/c" if you want to carry out tge  argument in cmd and  
// terminate
myProcess.StartInfo.Arguments = "/c dir";
// Allows to raise events
myProcess.EnableRaisingEvents = true;
//hosted by the application itself to not open a black cmd window
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.CreateNoWindow = true;
// Eventhander for data
myProcess.Exited += OnOutputDataRecived;
// Eventhandler for error
myProcess.ErrorDataReceived += OnErrorDataReceived;
// Eventhandler wich fires when exited
myProcess.Exited += OnExited;
// Starts the process
myProcess.Start();
//read the output before you wait for exit
myProcess.BeginOutputReadLine();
// wait for the finish - this will block (leave this out if you dont want to wait for 
// it, so it runs without blocking)
process.WaitForExit();

// Handle the dataevent
private void OnOutputDataRecived(object sender, DataReceivedEventArgs e)
{
    //do something with your data
    Trace.WriteLine(e.Data);
}

//Handle the error
private void OnErrorDataReceived(object sender, DataReceivedEventArgs e)
{        
    Trace.WriteLine(e.Data);
    //do something with your exception
    throw new Exception();
}    

// Handle Exited event and display process information.
private void OnExited(object sender, System.EventArgs e)
{
     Trace.WriteLine("Process exited");
}
 1
Author: Julian,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-05-18 10:07:14

Dla Zabawy, oto moje kompletne rozwiązanie do uzyskiwania danych wyjściowych Pythona-pod jednym kliknięciem przycisku - z raportowaniem błędów. Wystarczy dodać przycisk o nazwie "butPython" i etykietę o nazwie "llHello"...

    private void butPython(object sender, EventArgs e)
    {
        llHello.Text = "Calling Python...";
        this.Refresh();
        Tuple<String,String> python = GoPython(@"C:\Users\BLAH\Desktop\Code\Python\BLAH.py");
        llHello.Text = python.Item1; // Show result.
        if (python.Item2.Length > 0) MessageBox.Show("Sorry, there was an error:" + Environment.NewLine + python.Item2);
    }

    public Tuple<String,String> GoPython(string pythonFile, string moreArgs = "")
    {
        ProcessStartInfo PSI = new ProcessStartInfo();
        PSI.FileName = "py.exe";
        PSI.Arguments = string.Format("\"{0}\" {1}", pythonFile, moreArgs);
        PSI.CreateNoWindow = true;
        PSI.UseShellExecute = false;
        PSI.RedirectStandardError = true;
        PSI.RedirectStandardOutput = true;
        using (Process process = Process.Start(PSI))
            using (StreamReader reader = process.StandardOutput)
            {
                string stderr = process.StandardError.ReadToEnd(); // Error(s)!!
                string result = reader.ReadToEnd(); // What we want.
                return new Tuple<String,String> (result,stderr); 
            }
    }
 0
Author: Dave,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-01 12:45:13

Rozwiązanie Juliana jest testowane z kilkoma drobnymi poprawkami. Poniżej znajduje się przykład, który również użyty https://sourceforge.net/projects/bat-to-exe / GenericConsole.cs i https://www.codeproject.com/Articles/19225/Bat-file-compiler program.txt dla części args:

using System;
using System.Text;  //StringBuilder
using System.Diagnostics;
using System.IO;


class Program
{
    private static bool redirectStandardOutput = true;

    private static string buildargument(string[] args)
    {
        StringBuilder arg = new StringBuilder();
        for (int i = 0; i < args.Length; i++)
        {
            arg.Append("\"" + args[i] + "\" ");
        }

        return arg.ToString();
    }

    static void Main(string[] args)
    {
        Process prc = new Process();
        prc.StartInfo = //new ProcessStartInfo("cmd.exe", String.Format("/c \"\"{0}\" {1}", Path.Combine(Environment.CurrentDirectory, "mapTargetIDToTargetNameA3.bat"), buildargument(args)));
        //new ProcessStartInfo(Path.Combine(Environment.CurrentDirectory, "mapTargetIDToTargetNameA3.bat"), buildargument(args));
        new ProcessStartInfo("mapTargetIDToTargetNameA3.bat");
        prc.StartInfo.Arguments = buildargument(args);

        prc.EnableRaisingEvents = true;

        if (redirectStandardOutput == true)
        {
            prc.StartInfo.UseShellExecute = false;
        }
        else
        {
            prc.StartInfo.UseShellExecute = true;
        }

        prc.StartInfo.CreateNoWindow = true;

        prc.OutputDataReceived += OnOutputDataRecived;
        prc.ErrorDataReceived += OnErrorDataReceived;
        //prc.Exited += OnExited;

        prc.StartInfo.RedirectStandardOutput = redirectStandardOutput;
        prc.StartInfo.RedirectStandardError = redirectStandardOutput;

        try
        {
            prc.Start();
            prc.BeginOutputReadLine();
            prc.BeginErrorReadLine();
            prc.WaitForExit();
        }
        catch (Exception e)
        {
            Console.WriteLine("OS error: " + e.Message);
        }

        prc.Close();
    }

    // Handle the dataevent
    private static void OnOutputDataRecived(object sender, DataReceivedEventArgs e)
    {
        //do something with your data
        Console.WriteLine(e.Data);
    }

    //Handle the error
    private static void OnErrorDataReceived(object sender, DataReceivedEventArgs e)
    {
        Console.WriteLine(e.Data);
    }

    // Handle Exited event and display process information.
    //private static void OnExited(object sender, System.EventArgs e)
    //{
    //    var process = sender as Process;
    //    if (process != null)
    //    {
    //        Console.WriteLine("ExitCode: " + process.ExitCode);
    //    }
    //    else
    //    {
    //        Console.WriteLine("Process exited");
    //    }
    //}
}

Kod musi być skompilowany wewnątrz VS2007, używając polecenia csc.exe wygenerowany plik wykonywalny nie wyświetli poprawnie wyjścia konsoli, a nawet zawiesi się z błędem CLR20r3. Skomentuj wydarzenie OnExited proces, wyjście konsoli bat na exe będzie bardziej podobne do oryginalnego wyjścia konsoli bat.

 0
Author: Frank,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-07-10 13:51:58