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;
 }
相关推荐
2603_9658966210 分钟前
JavaScript 原型与原型链
开发语言·前端·javascript
朋友圈自动点赞工具16 分钟前
Ruby方法查找:对象不存方法
开发语言·前端·ruby·科技资讯
罗迪尼亚的熔岩21 分钟前
联合kuka机器人, 海康visonmaster 的手眼9点标定方法
机器人·c#·工业相机·标定
D e re k ZH25 分钟前
C++ 模板与泛型编程进阶(No 2)
开发语言·c++
一级新生28 分钟前
LLM _ Agent _ Skill _ 专家 的关系与选型笔记
java·开发语言·人工智能·笔记·大模型·智能体·skill
starzy199029 分钟前
Scala String操作:从插值器到正则的完整指南
开发语言·后端·scala
2501_9159090630 分钟前
Windows 打开 Xcode 工程,编译调试 Xcode 项目并和 Mac 同事协作
ide·windows·vscode·macos·ios·个人开发·xcode
普通人632 分钟前
CSS-box-sizing-使用指南
开发语言·前端·css·1024程序员节
她说彩礼65万33 分钟前
C# TaskCompletionSource
java·开发语言·c#
eggwyw39 分钟前
C++深入浅出探索数据结构的原理
开发语言·c++