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;
 }
相关推荐
geats人山人海16 分钟前
c# 第九章 record
开发语言·c#
早期的虫儿有鸟吃17 分钟前
vue2--Vuex 模块化
开发语言·前端·javascript
浪客川43 分钟前
Android的SystemUI的启动流程简析
android·开发语言
-银雾鸢尾-1 小时前
C#中的抽象类与抽象方法
开发语言·c#
萧瑟余晖1 小时前
JDK 20 新特性详解
java·开发语言
benchmark_cc1 小时前
如何用 Python 进行多周期 K 线合成与时区对齐?基于 QuantDash 与 Pandas 的量化数据清洗实战(附 GitHub 源码)
开发语言·python·github·盯盘·pandas·quantdash·量化数据
2301_794461571 小时前
Activiti/BPMN 2.0 的 4 种网关
java·服务器·开发语言
147API2 小时前
Claude Tag 进入 Slack 后,团队智能体需要哪些任务与审计字段
java·开发语言·数据库
熬夜苦读学习2 小时前
QT_信号和槽
开发语言·qt
代码雕刻家2 小时前
编程语法细节
java·c语言·开发语言