C#程序调用cmd执行多个命令
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox commandText;
Process p;
List<string> commandList;
int index;
public Form1()
{
InitializeComponent();
initProcess();
commandList = new List<string>();
}
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.commandText = new System.Windows.Forms.TextBox();
this.SuspendLayout();
this.button1.Location = new System.Drawing.Point(545, 10);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "按钮";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
this.textBox1.Location = new System.Drawing.Point(12, 39);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ReadOnly = true;
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.textBox1.Size = new System.Drawing.Size(608, 353);
this.textBox1.TabIndex = 1;
this.commandText.Location = new System.Drawing.Point(12, 12);
this.commandText.Name = "commandText";
this.commandText.Size = new System.Drawing.Size(527, 21);
this.commandText.TabIndex = 2;
this.commandText.KeyUp += new System.Windows.Forms.KeyEventHandler(this.commandText_KeyUp);
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(632, 404);
this.Controls.Add(this.commandText);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
private void initProcess() {
p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.OutputDataReceived += new DataReceivedEventHandler(cmdMessage);
p.ErrorDataReceived += new DataReceivedEventHandler(cmdMessage);
p.Start();
p.BeginOutputReadLine();
p.BeginErrorReadLine();
p.StandardInput.AutoFlush = true;
}
private void cmdMessage(object sender, DataReceivedEventArgs e) {
string data = e.Data;
setTextBox(data);
}
public void setTextBox(string msg) {
if (this.textBox1.InvokeRequired)
{
this.Invoke(new setMessage(setTextBox), msg);
}
else {
this.textBox1.Text += msg+"\r\n";
}
}
private void sendCommand(string command) {
if (!startCommand(command)) {
p.StandardInput.WriteLine(command);
}
}
private bool startCommand(string command)
{
string cmd = command.ToLower();
if (cmd.Equals("clear") || cmd.Equals("cls"))
{
textBox1.Text = "";
return true;
}
return false;
}
public delegate void setMessage(string msg);
private void button1_Click(object sender, EventArgs e)
{
string command = this.commandText.Text.Trim();
if (command.Length > 0) {
sendCommand(command);
commandList.Add(command);
index++;
this.commandText.Text = "";
}
}
private void commandText_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyValue == 13)
{
button1_Click(null, null);
}
else if (e.KeyValue == 38)
{
if (commandList.Count <= 0 || index <= 0) return;
index--;
this.commandText.Text = commandList[index];
}
else if (e.KeyValue == 40)
{
if (commandList.Count <= 0 || index >= commandList.Count) return;
index++;
this.commandText.Text = commandList[index];
}
}
}
}