Методы C# для получения сообщений об ошибках из команды CMD

Чтобы получить сообщение об ошибке от команды, выполненной в командной строке (CMD) с использованием C#, вы можете использовать класс Processиз пространства имен System.Diagnostics. Вот несколько методов, которые вы можете использовать:

Метод 1: фиксировать стандартную ошибку

using System;
using System.Diagnostics;
class Program
{
    static void Main()
    {
        string command = "your_command_here";
        string errorOutput;
        using (Process process = new Process())
        {
            process.StartInfo.FileName = "cmd.exe";
            process.StartInfo.Arguments = "/c " + command;
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.CreateNoWindow = true;
            process.Start();
            errorOutput = process.StandardError.ReadToEnd();
            process.WaitForExit();
        }
        Console.WriteLine(errorOutput);
    }
}

Метод 2: захват стандартного вывода и ошибок

using System;
using System.Diagnostics;
class Program
{
    static void Main()
    {
        string command = "your_command_here";
        string output, errorOutput;
        using (Process process = new Process())
        {
            process.StartInfo.FileName = "cmd.exe";
            process.StartInfo.Arguments = "/c " + command;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.CreateNoWindow = true;
            process.Start();
            output = process.StandardOutput.ReadToEnd();
            errorOutput = process.StandardError.ReadToEnd();
            process.WaitForExit();
        }
        Console.WriteLine(output);
        Console.WriteLine(errorOutput);
    }
}

Метод 3: асинхронный захват выходных данных и ошибок

using System;
using System.Diagnostics;
using System.Threading.Tasks;
class Program
{
    static async Task Main()
    {
        string command = "your_command_here";
        string output, errorOutput;
        using (Process process = new Process())
        {
            process.StartInfo.FileName = "cmd.exe";
            process.StartInfo.Arguments = "/c " + command;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.CreateNoWindow = true;
            process.Start();
            Task<string> outputTask = process.StandardOutput.ReadToEndAsync();
            Task<string> errorOutputTask = process.StandardError.ReadToEndAsync();
            output = await outputTask;
            errorOutput = await errorOutputTask;
            process.WaitForExit();
        }
        Console.WriteLine(output);
        Console.WriteLine(errorOutput);
    }
}

В этих примерах замените "your_command_here"фактической командой, которую вы хотите выполнить. Сообщение об ошибке будет зафиксировано в переменной errorOutput.