如何将参数传递给cmd.exe并将结果返回到C#Windows应用程序

时间:2015-06-26 06:16:43

标签: c#

我的程序的GUI。 enter image description here

我希望将参数作为 dir 传递给 cmd.exe ,并使用.NET Process类,并将正确的输出输出到c#程序中。< / p>

来源:

using System;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;

namespace MyCmd
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            comboBox1.Items.Add("cmd.exe");
            comboBox1.SelectedIndex = 0;
        }

        private Thread t;

        private void button1_Click(object sender, EventArgs e)
        {
            object[] param = new object[] { comboBox1.Text, txtParams.Text };
            if (param.Length > 0 && comboBox1.Text != "")
            {
                t = new Thread(() => doTask(param));
                t.IsBackground = true;
                t.Start();
            }
            else
            {
                MessageBox.Show("Invalid parameters!");
            }
        }

        private void doTask(object[] param)
        {
            Process proc = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName = param[0].ToString(),
                    Arguments = param[1].ToString(),
                    UseShellExecute = false,
                    RedirectStandardOutput = true,
                    CreateNoWindow = true
                }
            };
            proc.Start();
            while (!proc.StandardOutput.EndOfStream)
            {
                string line = proc.StandardOutput.ReadLine();
                this.Invoke((MethodInvoker)delegate
                {
                    txtResponse.Text += line + Environment.NewLine;
                });
            }
            proc.WaitForExit();
        }
    }
}

这是输出 enter image description here

我想启动 cmd.exe ,然后输入 dir ,然后将目录信息列入我的应用

更新:

来源更新:

using System;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;

namespace MyCmdV2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Add("cmd.exe");
            comboBox1.SelectedIndex = 0;
            proc = new Process();

            proc.StartInfo = new ProcessStartInfo
            {
                FileName = "cmd.exe",
                UseShellExecute = false,
                RedirectStandardOutput = true,
                RedirectStandardInput = true,
                CreateNoWindow = true
            };
            proc.Start();
        }

        private Process proc;

        private Thread t;

        private void button1_Click(object sender, EventArgs e)
        {
            t = new Thread(() => doIt());
            t.IsBackground = true;
            t.Start();
        }

        private void doIt()
        {
            if (txtParams.Text.ToLower() == "cls")
            {
                txtResponse.Text = "";
            }
            else
            {
                proc.StandardInput.WriteLine(@txtParams.Text);
                while (!proc.StandardOutput.EndOfStream)
                {
                    string line = proc.StandardOutput.ReadLine();
                    this.Invoke((MethodInvoker)delegate
                    {
                        txtResponse.Text += line + Environment.NewLine;
                    });
                }
                proc.WaitForExit();
            }
        }
    }
}

这只是一个示例查询。我需要使用参数 运行任何exe程序。 将我的所有程序列入ComboBox,然后传递TextBox中的参数。

有些时候我需要像这样将参数作为 IP 地址传递。 \\192.168.1.2

Ex: psinfo.exe \\192.168.1.2 

但是C#存储为\\\\192.168.1.2。如何删除参数中的转义序列字符。

3 个答案:

答案 0 :(得分:3)

你的代码看起来很好,因为它通过&#34; dir&#34;作为一个论点。您的代码与执行&#34; cmd.exe目录&#34;相同。您是否要启动cmd.exe然后输入dir以便列出目录结构?如果是这样,您需要像标准输出一样重定向标准输入,然后将dir写入输入流。

<强>更新

执行命令的代码:

    private void doTask(object[] param)
    {
        Process proc = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = param[0].ToString(),
                //remove this line, it's not needed
                //Arguments = param[1].ToString(),
                UseShellExecute = false,
                RedirectStandardOutput = true,
                //Add this line so you can write commands
                RedirectStandardInput = true,
                CreateNoWindow = true
            }
        };
        proc.Start();

        //now write your command with WriteLine so it mimics an enter press
        proc.StandardInput.WriteLine(param[1].ToString());

        while (!proc.StandardOutput.EndOfStream)
        {
            string line = proc.StandardOutput.ReadLine();
            this.Invoke((MethodInvoker)delegate
            {
                txtResponse.Text += line + Environment.NewLine;
            });
        }
        proc.WaitForExit();
    }

答案 1 :(得分:1)

似乎已经正确设置了标准输入和输出。如果您查看手册here,您应该更好地了解该怎么做。我使用Windows运行功能执行了两项测试。第一个是cmd dir,和你的一样,它失败了。第二个是cmd /K dir,它产生了我认为的预期输出。使用过的交换机使cmd窗口保持打开状态,检查命令完成后关闭进程的其他交换机的手册。

还有一点需要注意,虽然cmd中运行的大部分内容都是程序,但“dir”和“cd”之类的命令只是命令,你找不到dir.exe或{{ 1}}任何地方。

答案 2 :(得分:0)

调用cmd.exe时,需要使用/c参数。转到命令提示符并键入:

cmd.exe dir

这基本上就是您在应用程序中执行的操作,它提供与应用程序中相同的输出。 现在试试:

cmd.exe /c dir

这是您要查找的输出。因此,您告诉cmd.exe使用dir参数运行命令/c

所以,你的ProcessStartInfo.Arguments看起来像这样:

private void doTask(object[] param)
        {
            Process proc = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName = param[0].ToString(),
                    Arguments = "/c " + param[1].ToString(),
                    UseShellExecute = false,
                    RedirectStandardOutput = true,
                    CreateNoWindow = true
                }
            };
            proc.Start();

我已经重新创建了您的解决方案,这可以按预期工作。