Skip to main content
 首页 » 编程设计

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

2024年02月06日11虾米哥

我的程序的 GUI。

我想通过 .NET Process 类将参数作为 dir 传递给 cmd.exe 并获得正确输出到c#程序。

来源:

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(); 
        } 
    } 
} 

这是输出

我想启动 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。如何删除参数中的转义序列字符。

请您参考如下方法:

您的代码看起来不错,因为它确实将“dir”作为参数传递。您的代码与执行“cmd.exe dir”的效果相同。您是否要启动 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(); 
    }