简体   繁体   English

从批处理文件接收输出并输入

[英]Receive output from a batch file and input

I'm currently creating an application in C# that allows you to create a server and manage it easily, to do this it uses batch files to run said servers. 我目前正在用C#创建一个应用程序,它使您可以创建服务器并轻松管理它,为此,它使用批处理文件来运行所述服务器。 It works by creating batch files and using those to run the server. 它通过创建批处理文件并使用它们来运行服务器来工作。 (Java by the way). (顺便说一下Java)。

So, what I'm wondering is if it's possible to grab the output from the console and rather than dumping it in a textbox and closing the console like the code below does, I need it to continually post any output from the console without closing and without spamming loads of consoles open (tried using a timer). 因此,我想知道的是,是否有可能从控制台中获取输出,而不是像下面的代码那样将其转储到文本框中并关闭控制台,我需要它不断地发布控制台中的任何输出而不关闭和不会在不增加垃圾邮件的情况下打开控制台(使用计时器进行尝试)。

 Process myProcess = new Process();

        ProcessStartInfo myProcessStartInfo =
            new ProcessStartInfo(batchfilelocation);
        myProcessStartInfo.UseShellExecute = false;
        myProcessStartInfo.RedirectStandardOutput = true;

        myProcess.StartInfo = myProcessStartInfo;
        myProcess.Start();

        StreamReader myStreamReader = myProcess.StandardOutput;

        // Read the standard output of the spawned process.
        string myString = myStreamReader.ReadToEnd();


        textBox1.Text = textBox1.Text + myString;

        myProcess.Close();

I'm also wondering if it would be possible to allow input from a windows forms control such as a button that would execute a command by entering it in the console and pressing enter for example. 我也想知道是否可能允许来自Windows窗体控件的输入,例如通过在控制台中输入命令并按Enter键来执行命令的按钮。 Or a textbox that allows you to do the same thing. 或允许您执行相同操作的文本框。

Thanks! 谢谢!

You don't need a TextBox control to store a string. 您不需要TextBox控件来存储字符串。 Just use a string variable instead. 只需使用字符串变量即可。

string concatenatedString = null;

concatenatedString += myStreamReader.ReadToEnd();

Secondly, you can do this without creating a console window. 其次,您可以执行此操作而无需创建控制台窗口。 The code below will allow your program to run without seeing a ton of console windows popup. 下面的代码将允许您的程序运行,而不会看到大量的控制台窗口弹出窗口。

myProcessStartInfo.CreateNoWindow = true;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM