此代码演示了如何执行命令行进程并在Windows表单文本框中显示其实时输出。 让我们完善它,以清晰和鲁棒。
>
改进的代码:
using System; using System.Diagnostics; using System.Text; using System.Threading; using System.Windows.Forms; public static class CommandExecutor { public delegate void OutputHandler(string line); public static int Run(string workingDirectory, string command, string arguments, OutputHandler outputHandler, bool hideWindow = true) { int exitCode = -1; // Initialize to an invalid value try { using (var process = new Process()) { process.StartInfo.FileName = "cmd.exe"; process.StartInfo.WorkingDirectory = workingDirectory; process.StartInfo.Arguments = $"/c \"{command} {arguments}\" 2>&1"; // Redirect stderr to stdout process.StartInfo.CreateNoWindow = hideWindow; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; if (outputHandler != null) { process.OutputDataReceived += (sender, e) => { if (e.Data != null) { outputHandler(e.Data); } }; process.ErrorDataReceived += (sender, e) => { if (e.Data != null) { outputHandler($"Error: {e.Data}"); //Clearly mark error messages } }; } process.Start(); if (outputHandler != null) { process.BeginOutputReadLine(); process.BeginErrorReadLine(); //Begin reading error stream process.WaitForExit(); } else { process.WaitForExit(); } exitCode = process.ExitCode; } } catch (Exception ex) { MessageBox.Show($"An error occurred: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } return exitCode; } public static string GetOutput(string workingDirectory, string command, string arguments) { StringBuilder output = new StringBuilder(); Run(workingDirectory, command, arguments, line => output.AppendLine(line)); return output.ToString(); } } public partial class Form1 : Form { private TextBox txtOutput; //Declare TextBox public Form1() { InitializeComponent(); txtOutput = new TextBox { Dock = DockStyle.Fill, Multiline = true, ScrollBars = ScrollBars.Both }; Controls.Add(txtOutput); // Add the TextBox to the form //Add a button (btnExecute) to your form in the designer. } private void btnExecute_Click(object sender, EventArgs e) { //Get command and arguments from your textboxes (e.g., textBoxCommand, textBoxArguments) string command = textBoxCommand.Text; string arguments = textBoxArguments.Text; CommandExecutor.Run(@"C:\", command, arguments, line => { if (txtOutput.InvokeRequired) { txtOutput.Invoke(new MethodInvoker(() => txtOutput.AppendText(line + Environment.NewLine))); } else { txtOutput.AppendText(line + Environment.NewLine); } }); } }密钥改进:
>
- 错误处理:
- > 块在过程执行过程中处理潜在的异常。
try-catch
>更清晰的命名: - 更多描述性变量名称可提高可读性(,)。
CommandExecutor
workingDirectory
错误流处理: - >代码现在读取并显示命令中的错误输出。
> >线程安全: 检查从背景线程更新文本框时确保线程安全。 -
emoventir.newline:
InvokeRequired
>使用 用于跨平台一致的线路断裂。 -
简化的参数处理:
Environment.NewLine
使用字符串插值进行清洁参数构建。 - >单独的文本框声明:单独声明文本框以分别为更好的组织声明。>
- 请记住在Visual Studio Designer中添加一个文本框(例如,)和一个按钮(例如,)。 您还需要文本框来输入命令及其参数。 用文本框的实际名称替换 和
以上是如何在 Windows 窗体文本框中显示实时命令输出?的详细内容。更多信息请关注PHP中文网其他相关文章!

本文解释了C标准模板库(STL),重点关注其核心组件:容器,迭代器,算法和函子。 它详细介绍了这些如何交互以启用通用编程,提高代码效率和可读性t

本文详细介绍了c中有效的STL算法用法。 它强调了数据结构选择(向量与列表),算法复杂性分析(例如,std :: sort vs. std vs. std :: partial_sort),迭代器用法和并行执行。 常见的陷阱

本文详细介绍了C中的有效异常处理,涵盖了尝试,捕捉和投掷机制。 它强调了诸如RAII之类的最佳实践,避免了不必要的捕获块,并为强大的代码登录例外。 该文章还解决了Perf

本文讨论了使用C中的移动语义来通过避免不必要的复制来提高性能。它涵盖了使用std :: Move的实施移动构造函数和任务运算符,并确定了关键方案和陷阱以有效

C 20范围通过表现力,合成性和效率增强数据操作。它们简化了复杂的转换并集成到现有代码库中,以提高性能和可维护性。

本文讨论了C中的动态调度,其性能成本和优化策略。它突出了动态调度会影响性能并将其与静态调度进行比较的场景,强调性能和之间的权衡

文章讨论了在C中有效使用RVALUE参考,以进行移动语义,完美的转发和资源管理,重点介绍最佳实践和性能改进。(159个字符)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

Atom编辑器mac版下载
最流行的的开源编辑器

Dreamweaver CS6
视觉化网页开发工具

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器