自己写的,有效
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
string cmd = @"system info";
ExcuteCmd(cmd);
}
catch (System.Exception ex)
{
}
}
/// <summary>
/// 执行Cmd命令
/// </summary>
public string ExcuteCmd(string c, string workDirectory=null)
{
//创建执行cmd
System.Diagnostics.Process process = new System.Diagnostics.Process();
//process.StartInfo.FileName = "cmd.exe";
process.StartInfo.FileName = @"C:\Windows\System32\cmd.exe";
//process.StartInfo.WorkingDirectory = workDirectory;
string cmdPath = @"c:\Users\vi.ta";//关键这里要跳到cmd执行命令的这里,cmd窗口使用的目录
if(textBoxPath.Text=="")
{
MessageBox.Show("请输入要执行的命令的目录");
//return null;//这里加了一个判断,根据不同用户的目录不同,输入'
}
else
{
cmdPath = textBoxPath.Text;
}
process.StartInfo.WorkingDirectory = cmdPath;//这个地方决定了运行的路径
//p.startinfo.arguments="cd " + Application.StartPath;//改变路径
//配置开发方式输入输出错误
process.StartInfo.UseShellExecute = false;//不启动shell启动程序
process.StartInfo.CreateNoWindow = true;//不创建新窗口
process.StartInfo.RedirectStandardOutput = true;//重定向错误输出
process.StartInfo.RedirectStandardInput = true;//重定向输入
//执行cmd且获取返回值
process.Start();
//当程序输入python时,系统进入到python中,所以没有反应
process.StandardInput.WriteLine(c);//向cmd窗口发送信息 ,后面+&exit表明运行后马上退出
process.StandardInput.AutoFlush = true;//提交
process.StandardInput.WriteLine("exit");
//读取返回值
StreamReader reader = process.StandardOutput;//截取输出流
string output = reader.ReadLine();//每次读取一行
while (!reader.EndOfStream)
{
//PrintThrendInfo(output);
output += reader.ReadLine();
PrintMsg(output);
}
process.WaitForExit();//等待程序进行完退出程序
return output; //输出返回值
}
/// <summary>
/// 打印信息至文本框
/// </summary>
/// <param name="msg">要打印的信息</param>
/// <param name="flag">true则在信息末尾打印回车,false不打印回车</param>
public void PrintMsg(string msg, bool flag = true)
{
try
{
this.richTextBox_ReceMsg.BeginInvoke((Action)delegate
{
//如果RichTextBox已接受文本字符超过50000个字符则清空
if (richTextBox_ReceMsg.Text.Length >= 100000)//richTextBox_ReceMsg.MaxLength/50000
{
richTextBox_ReceMsg.Clear();
}
if (flag)
{
richTextBox_ReceMsg.AppendText(msg + "\n");
}
else
richTextBox_ReceMsg.AppendText(msg);
//光标滚动到最后一行
this.richTextBox_ReceMsg.ScrollToCaret();
});
}
catch (System.Exception ex)
{
richTextBox_ReceMsg.AppendText(ex.Message + "\n");
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
string cmd = @"ifconfig/all";
ExcuteCmd(cmd);
}
catch (System.Exception ex)
{
MessageBox.Show("测试TestThumbstick up 失败" + ex.Message);
}
}
后面的来自网上转载
C#两种处理cmd的方式
public string startcmdOne(string command)
{
string output = "";
try
{
Process cmd = new Process();
cmd.StartInfo.FileName = command;
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
cmd.Start();
output = cmd.StandardOutput.ReadToEnd();
PrintMsg(output);
cmd.WaitForExit();
cmd.Close();
}
catch (Exception e)
{
MessageBox.Show("startcmdOne error" + e.Message);
}
return output;
}
public string startcmdTwo(string command, string argument)
{
string output = "";
try
{
Process cmd = new Process();
cmd.StartInfo.FileName = command;
cmd.StartInfo.Arguments = argument;
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
cmd.Start();
output = cmd.StandardOutput.ReadToEnd();
PrintMsg(output);
cmd.WaitForExit();
cmd.Close();
}
catch (Exception e)
{
MessageBox.Show("startcmdTwo error" + e.Message);
}
return output;
}