Добавь к этому коду, чтобы в Message.Box выводились сообщения из python private void…
- User
Добавь к этому коду, чтобы в Message.Box выводились сообщения из python private void button2_Click(object sender, EventArgs e) { string baseDirectory = AppDomain.CurrentDomain.BaseDirectory; string scriptPath = @“Scripts\imports.py”; var psi = new ProcessStartInfo { FileName = “python”, Arguments = scriptPath, // Убедитесь, что путь в кавычках, если есть пробелы WorkingDirectory = baseDirectory, UseShellExecute = false, CreateNoWindow = true, RedirectStandardOutput = true, RedirectStandardError = true };
- Assistant
using System.Diagnostics;
private void button2_Click(object sender, EventArgs e) { string baseDirectory = AppDomain.CurrentDomain.BaseDirectory; string scriptPath = @"Scripts\imports.py"; var psi = new ProcessStartInfo { FileName = "python", Arguments = scriptPath, WorkingDirectory = baseDirectory, UseShellExecute = false, CreateNoWindow = true, RedirectStandardOutput = true, RedirectStandardError = true };
var process = new Process(); process.StartInfo = psi; process.OutputDataReceived += Process_OutputDataReceived; process.ErrorDataReceived += Process_ErrorDataReceived; process.Start(); process.BeginOutputReadLine(); process.BeginErrorReadLine(); process.WaitForExit();}
private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e) { if (!string.IsNullOrEmpty(e.Data)) { MessageBox.Show(e.Data); } }
private void Process_ErrorDataReceived(object sender, DataReceivedEventArgs e) { if (!string.IsNullOrEmpty(e.Data)) { MessageBox.Show(e.Data); } }
- User
Должны выводится только сообщения python, а не весь происходящий процесс
- Assistant
private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e) { if (!string.IsNullOrEmpty(e.Data)) { MessageBox.Show(e.Data, "Python Output", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
private void Process_ErrorDataReceived(object sender, DataReceivedEventArgs e) { if (!string.IsNullOrEmpty(e.Data)) { MessageBox.Show(e.Data, "Python Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
- User
Все равно выдает множество окон с сообщением, а нужно только то что выдается в python
- Assistant
private StringBuilder outputBuilder = new StringBuilder();
private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e) { if (!string.IsNullOrEmpty(e.Data)) { outputBuilder.AppendLine(e.Data); } }
private void Process_ErrorDataReceived(object sender, DataReceivedEventArgs e) { if (!string.IsNullOrEmpty(e.Data)) { outputBuilder.AppendLine(e.Data); } }
private void button2_Click(object sender, EventArgs e) { string baseDirectory = AppDomain.CurrentDomain.BaseDirectory; string scriptPath = @"Scripts\imports.py"; var psi = new ProcessStartInfo { FileName = "python", Arguments = scriptPath, WorkingDirectory = baseDirectory, UseShellExecute = false, CreateNoWindow = true, RedirectStandardOutput = true, RedirectStandardError = true };
var process = new Process(); process.StartInfo = psi; process.OutputDataReceived += Process_OutputDataReceived; process.ErrorDataReceived += Process_ErrorDataReceived; process.Start(); process.BeginOutputReadLine(); process.BeginErrorReadLine(); process.WaitForExit(); string output = outputBuilder.ToString(); MessageBox.Show(output, "Python Output", MessageBoxButtons.OK, MessageBoxIcon.Information);