Проблемы с кодировкой в WinForms

Мне нужно запустить консольные проекты по кнопке на форме. Попробовал это сделать через richTextBox, перенаправил из процесса вывод на него. В половине проектов нормально выводит,в другой половине выводит каракули. Хотя я в каждом проекте пробовал ставить кодировку на вывод и ввод с консоли на UTF8. Скриншот прикрепил.

Form1.cs:

using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;

namespace CSLab3
{
   
    public partial class Form1 : Form
    {
        private Process process;
        public Form1()
        {
            InitializeComponent();
        }
        private void btnStartProcesses_Click(object sender, EventArgs e)
        {
            process = new Process();
            string fileName = ((Button)sender).Name;
            string noExt = Path.GetFileNameWithoutExtension(fileName);
            process.StartInfo.FileName = "C:\\Users\\Максим\\source\\repos\\"+noExt+"\\"+noExt+"\\bin\\Debug\\net7.0\\"+fileName;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardInput = true;
            process.OutputDataReceived += new DataReceivedEventHandler(OutputDataReceived);
            process.Start();
            process.BeginOutputReadLine();

            richTextBox1.KeyPress += new KeyPressEventHandler(richTextBox1_KeyPress);
            richTextBox1.Focus();
        }

    

        private void OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            richTextBox1.Invoke((MethodInvoker)delegate
            {
                richTextBox1.SelectionColor = Color.Green;
                string text = e.Data + "\n";
                byte[] bytes = Encoding.UTF8.GetBytes(text);
                string utf8String = Encoding.UTF8.GetString(bytes);
                richTextBox1.AppendText(utf8String);
                richTextBox1.ScrollToCaret();
            });

        }

        private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            // Передаем введенную команду в процесс
            if (e.KeyChar == (char)Keys.Enter)
            {
                if (process1!= null && !process.HasExited)
                {
                    process.StandardInput.WriteLine(richTextBox1.Text);
                }
                richTextBox1.Text = "";
                e.Handled = true;
            }
        }

    }
}

Form1.Designer.cs:

namespace CSLab3
{
    partial class Form1
    {
        private System.ComponentModel.IContainer components = null;

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.richTextBox1 = new System.Windows.Forms.RichTextBox();
            this.process1 = new System.Windows.Forms.Button();
            this.process2 = new System.Windows.Forms.Button();
            this.process3 = new System.Windows.Forms.Button();
            this.process4 = new System.Windows.Forms.Button();
            this.process5 = new System.Windows.Forms.Button();

            this.SuspendLayout();
            // 
            // richTextBox1
            // 
            this.richTextBox1.Location = new System.Drawing.Point(12, 12);
            this.richTextBox1.Name = "richTextBox1";
            this.richTextBox1.Size = new System.Drawing.Size(776, 397);
            this.richTextBox1.TabIndex = 0;
            this.richTextBox1.Text = "";
            
            //
            // btnStartProcesses
            //
            this.process1.Location = new System.Drawing.Point(12, 415);
            this.process1.Name = "CS_Lab1.exe";
            this.process1.Size = new System.Drawing.Size(120, 23);
            this.process1.TabIndex = 1;
            this.process1.Text = "Лаба 1";
            this.process1.UseVisualStyleBackColor = true;
            this.process1.Click += new System.EventHandler(this.btnStartProcesses_Click);
            //
            // btnStartProcesses
            //
            this.process2.Location = new System.Drawing.Point(142, 415);
            this.process2.Name = "CS_Lab2.exe";
            this.process2.Size = new System.Drawing.Size(120, 23);
            this.process2.TabIndex = 1;
            this.process2.Text = "Лаба 2";
            this.process2.UseVisualStyleBackColor = true;
            this.process2.Click += new System.EventHandler(this.btnStartProcesses_Click);
            //
            // btnStartProcesses
            //
            this.process3.Location = new System.Drawing.Point(272, 415);
            this.process3.Name = "CS_Lab3.exe";
            this.process3.Size = new System.Drawing.Size(120, 23);
            this.process3.TabIndex = 1;
            this.process3.Text = "Лаба 3";
            this.process3.UseVisualStyleBackColor = true;
            this.process3.Click += new System.EventHandler(this.btnStartProcesses_Click);
            //
            // btnStartProcesses
            //
            this.process4.Location = new System.Drawing.Point(402, 415);
            this.process4.Name = "CS_Lab5.exe";
            this.process4.Size = new System.Drawing.Size(120, 23);
            this.process4.TabIndex = 1;
            this.process4.Text = "Лаба 5";
            this.process4.UseVisualStyleBackColor = true;
            this.process4.Click += new System.EventHandler(this.btnStartProcesses_Click);
            //
            // btnStartProcesses
            //
            this.process5.Location = new System.Drawing.Point(532, 415);
            this.process5.Name = "CS_Lab6.exe";
            this.process5.Size = new System.Drawing.Size(120, 23);
            this.process5.TabIndex = 1;
            this.process5.Text = "Лаба 6";
            this.process5.UseVisualStyleBackColor = true;
            this.process5.Click += new System.EventHandler(this.btnStartProcesses_Click);

            //
            // MainForm
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.process1);
            this.Controls.Add(this.process2);
            this.Controls.Add(this.process3);
            this.Controls.Add(this.process4);
            this.Controls.Add(this.process5);
            this.Controls.Add(this.richTextBox1);
            this.Name = "MainForm";
            this.Text = "Form1";
            this.ResumeLayout(false);
        }

        #endregion

        private System.Windows.Forms.RichTextBox richTextBox1;
        private System.Windows.Forms.Button process1,process2,process3,process4,process5;

    }
}

[![введите сюда описание изображения][1]][1]
[1]: https://i.stack.imgur.com/p368b.png


Ответы (0 шт):