C#执行命令行

效果图

主要代码方法

cs 复制代码
 private Process p;
 public List<string> ExecuteCmd(string args)
 {
     System.Diagnostics.Process p = new System.Diagnostics.Process();
     p.StartInfo.FileName = "cmd.exe";
     p.StartInfo.RedirectStandardInput = true;
     p.StartInfo.RedirectStandardOutput = true;
     p.StartInfo.RedirectStandardError = true;
     p.StartInfo.CreateNoWindow = true;
     p.StartInfo.UseShellExecute = false;
     p.StartInfo.StandardOutputEncoding = System.Text.Encoding.UTF8;
     //p.StartInfo.StandardErrorEncoding = System.Text.Encoding.UTF8;
     p.Start();

     p.StandardInput.WriteLine(args + "&exit");

     p.StandardInput.AutoFlush = true;
     //p.StandardInput.WriteLine("exit");

     var list = new List<string>();
     StreamReader reader = p.StandardOutput;
     string line = reader.ReadLine();
     while (!reader.EndOfStream)
     {
         list.Add(line);
         line = reader.ReadLine();
     }

     p.WaitForExit();
     p.Close();
     return list;
 }
相关推荐
luofeiju5 分钟前
交叉编译笔记
开发语言
StudyWinter34 分钟前
【C++】仿函数和回调函数
开发语言·c++·回调函数·仿函数
C4程序员1 小时前
北京JAVA基础面试30天打卡14
java·开发语言·面试
黑客影儿1 小时前
Go特有的安全漏洞及渗透测试利用方法(通俗易懂)
开发语言·后端·安全·web安全·网络安全·golang·系统安全
你好,我叫C小白2 小时前
C语言 常量,数据类型
c语言·开发语言·数据类型·常量
昏睡红猹2 小时前
从0.99到1实现一个Windows上的虚拟hid键盘设备
windows·windows driver
小红帽2.02 小时前
从ioutil到os:Golang在线客服聊天系统文件读取的迁移实践
服务器·开发语言·golang
Zafir20243 小时前
Qt实现TabWidget通过addTab函数添加的页,页内控件自适应窗口大小
开发语言·c++·qt·ui
阿巴~阿巴~3 小时前
深入解析C++非类型模板参数
开发语言·c++
谷宇.4 小时前
【Unity3D实例-功能-拔枪】角色拔枪(三)IK的使用-紧握武器
游戏·unity·c#·unity3d·游戏开发·游戏编程·steam