C#实现cmd命令的相同效果
来源:互联网 发布:手机歌曲剪辑软件 编辑:程序博客网 时间:2024/06/05 00:11
自己写的,有效
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; }
阅读全文
0 0
- C#实现cmd命令的相同效果
- C#实现Cmd命令
- C#执行cmd命令实现电脑关机
- 利用ajax持续发送请求,实现web版的cmd命令执行效果
- C#调用cmd里的各种命令
- c#执行windows的cmd命令操作
- C# 调用 CMD 命令 遇到的问题
- C#运行cmd命令
- C#调用CMD命令
- C# 执行CMD命令
- C# 调用 CMD 命令
- C# 执行CMD命令
- C#执行CMD命令
- c# 执行CMD命令
- C#调用cmd命令
- c#执行cmd命令
- c#调用cmd命令
- 用C#来调用cmd命令程序,实现wifi控制的工具
- iOS转前端之CSS基础学习
- Python __str__(self)和__unicode__(self)
- jQuery实现的增删改查
- 精通比特币
- oracle数据类型和对应的java类型
- C#实现cmd命令的相同效果
- MVC框架-mentawai(6)
- scrapy 元素的相对xpath
- AMD 和 CMD 的区别有哪些?
- TCP_NODELAY 和 TCP_NOPUSH的解释
- pytesseract 报windows err no2的错误
- python使用tkinter写带界面的工具
- python使用pyqt写带界面工具
- 基于pyteseract google ocr的图形验证码识别