1

Есть код:

internal static void Run(string task, RichTextBox output)
{
        string path = Path.GetFullPath("Project");
        Console.WriteLine($"/c cd \"{path}\" & jppm {task}");
        Process p = new Process();
        p.StartInfo.FileName = "cmd.exe";
        p.StartInfo.Arguments = $"/c cd \"{path}\" & jppm {task}";
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        p.Start();
        string line = null;
        while((line = p.StandardOutput.ReadLine()) != null)
        {
            byte[] bytes = Encoding.Default.GetBytes(p.StandardOutput.ReadLine());
            string result = Encoding.GetEncoding("cp866").GetString(bytes);
            output.Document.Blocks.Add(new Paragraph(new Run(result)));
        }
 }


Строка: Console.WriteLine($"/c cd \"{path}\" & jppm {task}"); выводит:

/c cd "C:\Users\deadmoz5er\source\repos\IDE\IDE\bin\Debug\Project" & jppm app:run

Если эту команду я выполню в CMD в ручную, то все ок, но Visual Studio кидает ошибку:

Вызвано исключение: "System.ArgumentNullException" в mscorlib.dll Необработанное исключение типа "System.ArgumentNullException" в mscorlib.dll Ссылка на строку не ссылается на экземпляр String.

Ошибка в строке: byte[] bytes = Encoding.Default.GetBytes(p.StandardOutput.ReadLine());

Как решить?

2 ответа 2

0
byte[] bytes = Encoding.Default.GetBytes(line);
0

[РЕШЕНО]

Код:

internal static void Run(string task, RichTextBox output)
{
        string path = Path.GetFullPath("Project");
        Console.WriteLine("/c cd \"{path}\" & jppm {task}");
        Process p = new Process();
        p.StartInfo.FileName = "cmd.exe";
        p.StartInfo.Arguments = $"/c cd \"{path}\" & jppm {task}";
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        p.Start();
        string line = "";
        while((line = p.StandardOutput.ReadLine()) != null)
        {
            byte[] bytes = Encoding.Default.GetBytes(line);
            string result = Encoding.GetEncoding("cp866").GetString(bytes);
            output.Document.Blocks.Add(new Paragraph(new Run(result)));
        }
}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.