【问题标题】:Console within form Visual C#窗体 Visual C# 中的控制台
【发布时间】:2018-11-23 17:06:26
【问题描述】:

我想在我的表单中获得一个控制台窗口。基本上,当您单击button1 时,它会运行一个批处理脚本(test.exe)。我不想要一个单独的批处理窗口,而是希望它显示在我的表单中。

我认为可能有两种方法可以做到这一点,1,以某种方式将控制台嵌入到我的表单中,或者 2,当您单击 button1 时设置 StartInfo.CreateNoWindow = true; 并将输出汇集到列表框中以模拟在我的表单中进行控制台。

我只是有点卡住了,因为我已经找到了两种方法,但是我自己用人们建议的各种其他方法进行了测试,但没有任何效果。但无论哪种方式,我的用户都需要能够将输入发送回控制台。

哪种方法更简单,我该怎么做?

【问题讨论】:

  • @JNevill 是的,但我什至无法让它与一个全新的项目一起工作。除非我不理解某些东西,否则这个家伙提供的一半设置代码只是没有在任何地方定义的变量,或者对于它应该与什么相关的没有意义的代码。
  • @Koder101 不,这不是我要找的
  • 他那里有完整的源代码、二进制文件和示例应用程序(虽然我没有看过,所以你可能是对的)。它似乎是一个相当简单的库,您可以通过 nuget 安装。

标签: c#


【解决方案1】:

我相信最好的方法是redirect output。基本上事情仍然会按照你的意愿执行,但你会在任何你想要/需要的地方得到输出。

【讨论】:

    【解决方案2】:
    using System;
    using System.Diagnostics;
    using System.Text;
    using System.Windows.Forms;
    
    namespace ConsoleOutput_test
    {
    public partial class Form1 : Form
    {
        Process sortProcess;
        private static StringBuilder sortOutput = null;
    
        public Form1()
        {
            InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            sortProcess = new Process();
            sortProcess.StartInfo.FileName = "C:\\Windows\\System32\\cmd.exe";
            // Set UseShellExecute to false for redirection.
            sortProcess.StartInfo.CreateNoWindow = true;
            sortProcess.StartInfo.UseShellExecute = false;
    
            // Redirect the standard output of the sort command.  
            // This stream is read asynchronously using an event handler.
            sortProcess.StartInfo.RedirectStandardOutput = true;
            sortProcess.StartInfo.RedirectStandardInput = true;
            sortProcess.StartInfo.RedirectStandardError = true;
            sortOutput = new StringBuilder("");
    
            // Set our event handler to asynchronously read the sort output.
            sortProcess.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);
            sortProcess.ErrorDataReceived += new DataReceivedEventHandler(SortErrorHandler);
    
            // Redirect standard input as well.  This stream
            // is used synchronously.
            sortProcess.StartInfo.RedirectStandardInput = true;
    
            // Start the process.
            sortProcess.Start();
    
            // Start the asynchronous read of the sort output stream.
            sortProcess.BeginOutputReadLine();
            while (!sortProcess.HasExited)
            {
                Application.DoEvents(); // This keeps your form responsive by processing events
            }
        }
    
        private void SortOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
        {
            if (txtConsole.InvokeRequired) { txtConsole.BeginInvoke(new DataReceivedEventHandler(SortOutputHandler), new[] { sendingProcess, outLine }); }
            else
            {
                txtConsole.AppendText(Environment.NewLine + outLine.Data);
            }
        }
        private void SortErrorHandler(object sendingProcess, DataReceivedEventArgs outLine)
        {
            if (txtConsole.InvokeRequired) { txtConsole.BeginInvoke(new DataReceivedEventHandler(SortErrorHandler), new[] { sendingProcess, outLine }); }
            else
            {
                txtConsole.AppendText(Environment.NewLine + outLine.Data);
            }
        }
    
    
        private void button2_Click(object sender, EventArgs e)
        {
            sortProcess.StandardInput.WriteLine(txtOutput.Text);
            txtOutput.Text = "";
        }
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-18
      • 1970-01-01
      • 2014-01-05
      • 1970-01-01
      • 2016-05-17
      • 2012-02-08
      • 2015-03-03
      相关资源
      最近更新 更多