C#-Windows窗体-输入和cmd.exe外壳程序的输出 [英] C# - Windows Forms - Input & Output from cmd.exe shell

查看:125
本文介绍了C#-Windows窗体-输入和cmd.exe外壳程序的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试创建一个与命令提示符外壳(cmd.exe)交互的Windows Forms C#项目.

我想打开命令提示符,发送命令(例如ipconfig),然后将结果读回到Windows窗体中,变成字符串,文本框或其他内容.

到目前为止,这是我所拥有的,但是我仍然被困住了.我无法写入或读取命令提示符.

Hi,

I am trying to create a Windows Forms C# project that interacts with the command prompt shell (cmd.exe).

I want to open a command prompt, send a command (like ipconfig) and then read the results back into the windows form into a string, textbox, or whatever.

Here is what I have so far, but I am stuck. I cannot write or read to the command prompt.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;


namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
           
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.Arguments = "/k dir *.*";
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            
            p.Start();

            StreamWriter inputWriter = p.StandardInput;
            StreamReader outputWriter = p.StandardOutput;
            StreamReader errorReader = p.StandardError;
            p.WaitForExit();
            
        }
    }
}



任何帮助将不胜感激.

谢谢.



Any help would be greatly appreciated.

Thanks.

推荐答案

我看不到您试图在重定向流中写入或读取任何内容的位置.为什么引入变量inputWriteroutputWritererrorReader?首先,您不需要它们,因为您可以访问p.StandardInputp.StandardOutputp.StandardError.更重要的是,您没有使用这些字符串.

当您使用"CMD.EXE/K"时,您无法在同一"CMD.EXE"运行期间写入其他命令,因为此选项会使命令解释器仅执行参数中的命令并退出.如果您未使用"/K",则可以使用p.StandardInput发送多个命令,直到您命令退出"为止.在所有情况下,您都可以从流p.StandardOutputp.StandardError收集所有输出读数.
就做吧.

请参见此处的代码示例: http://msdn.microsoft.com/zh-我们/library/system.diagnostics.processstartinfo.redirectstandardoutput.aspx [ ^ ].

—SA
I don''t see where you''re trying to write or read anything to/from redirected streams. Why did you introduced the variables inputWriter, outputWriter and errorReader? First, you don''t need them, as you have access to p.StandardInput, p.StandardOutput and p.StandardError. More importantly, you''re not using those strings.

As you''re using "CMD.EXE /K", you cannot write another command during the same "CMD.EXE" run time, as this options causes the command interpreter to execute only the command from the parameter and exit. If you did not use "/K" you could use p.StandardInput to send several commands until you command "exit". In all cases, you can collect all output reading from the streams p.StandardOutput and p.StandardError.

Just do it.

See the code sample here: http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput.aspx[^].

—SA


这篇关于C#-Windows窗体-输入和cmd.exe外壳程序的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆