ZBLOG

using System; using System.Diagnostics; using System.Windows.Forms; namespace CmdExecutor { public partial class MainForm : Form { public MainForm() { InitializeComponent(); InitializeComboBox(); ...

您可以在 ExecuteCommand 方法中添加一个关闭窗体的逻辑判断。具体来说,您可以使用 FormClosing 事件来处理用户尝试关闭窗体的情况。在这个事件中,您可以询问用户是否确定要关闭窗体,并相应地处理命令的执行状态。

下面是经过修改后的代码示例:

  1. using System;
  2. using System.Diagnostics;
  3. using System.Threading.Tasks;
  4. using System.Windows.Forms;
  5. namespace CmdExecutor
  6. {
  7. public partial class MainForm : Form
  8. {
  9. private Process cmdProcess; // 用于保存当前执行的cmd进程
  10. public MainForm()
  11. {
  12. InitializeComponent();
  13. InitializeComboBox();
  14. }
  15. private void InitializeComboBox()
  16. {
  17. // 添加可选的CMD命令
  18. comboBoxCommands.Items.Add("ping");
  19. comboBoxCommands.Items.Add("ipconfig");
  20. comboBoxCommands.Items.Add("mkdir");
  21. }
  22. private void buttonExecute_Click(object sender, EventArgs e)
  23. {
  24. // 获取选中的命令和用户输入的参数
  25. string selectedCommand = comboBoxCommands.SelectedItem?.ToString();
  26. string additionalParams = textBoxCommandInput.Text.Trim();
  27. // 检查是否选择了命令以及额外参数是否输入
  28. if (string.IsNullOrEmpty(selectedCommand))
  29. {
  30. MessageBox.Show("请从下拉列表中选择一个CMD命令!");
  31. return;
  32. }
  33. if (string.IsNullOrEmpty(additionalParams))
  34. {
  35. MessageBox.Show("请输入有效的额外参数!");
  36. return;
  37. }
  38. // 组合完整的CMD命令,以空格分隔
  39. string command = $"{selectedCommand} {additionalParams}".Trim();
  40. ExecuteCommand(command);
  41. }
  42. private async void ExecuteCommand(string command)
  43. {
  44. textBoxLog.Clear();
  45. ProcessStartInfo processStartInfo = new ProcessStartInfo
  46. {
  47. FileName = "cmd.exe",
  48. RedirectStandardInput = true,
  49. RedirectStandardOutput = true,
  50. RedirectStandardError = true,
  51. UseShellExecute = false,
  52. CreateNoWindow = true
  53. };
  54. cmdProcess = new Process { StartInfo = processStartInfo };
  55. cmdProcess.OutputDataReceived += (sender, args) =>
  56. AppendLog(args.Data);
  57. cmdProcess.ErrorDataReceived += (sender, args) =>
  58. AppendLog(args.Data);
  59. cmdProcess.Start();
  60. process.BeginOutputReadLine();
  61. process.BeginErrorReadLine();
  62. await Task.Run(() =>
  63. {
  64. process.StandardInput.WriteLine(command);
  65. process.StandardInput.WriteLine("exit");
  66. process.WaitForExit();
  67. });
  68. }
  69. private void AppendLog(string message)
  70. {
  71. if (!string.IsNullOrEmpty(message))
  72. {
  73. if (textBoxLog.InvokeRequired)
  74. {
  75. textBoxLog.Invoke(new Action<string>(AppendLog), message);
  76. }
  77. else
  78. {
  79. textBoxLog.AppendText(message + Environment.NewLine);
  80. textBoxLog.ScrollToCaret();
  81. }
  82. }
  83. }
  84. protected override void OnFormClosing(FormClosingEventArgs e)
  85. {
  86. base.OnFormClosing(e);
  87. if (cmdProcess != null && !cmdProcess.HasExited)
  88. {
  89. var result = MessageBox.Show("正在执行CMD命令,确认要关闭吗?", "确认", MessageBoxButtons.YesNo);
  90. if (result == DialogResult.Yes)
  91. {
  92. // 终止进程并关闭窗体
  93. cmdProcess.Kill();
  94. e.Cancel = false; // 允许关闭窗体
  95. }
  96. else
  97. {
  98. e.Cancel = true; // 取消关闭操作
  99. }
  100. }
  101. }
  102. }
  103. }

修改说明:

  1. 新字段:在 MainForm 类中添加了一个私有字段 cmdProcess 来保存当前正在执行的 CMD 进程。

  2. 更新 ExecuteCommand 方法:将启动的进程存储在 cmdProcess 中,以便后续引用。

  3. 重写 OnFormClosing 方法

    • 在窗口关闭时检查 CMD 进程是否仍在运行。
    • 如果是,则显示一个确认对话框,让用户决定是否要终止进程并关闭窗口。
    • 根据用户选择,通过设置 e.Cancel 属性来控制窗口是否继续关闭。

这样,当您尝试关闭窗口时,如果 CMD 命令仍在运行,会提示用户进行确认。如果用户选择“是”,将终止该进程并关闭窗口;如果选择“否”,则继续执行 CMD 命令。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=19557

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?