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;
 }
相关推荐
csbysj2020几秒前
中介者模式
开发语言
hsjkdhs1 小时前
C++之类的继承与派生
开发语言·c++
lly2024062 小时前
HTML 元素:构建网页的基础
开发语言
低调小一2 小时前
LRU缓存科普与实现(Kotlin 与 Swift)
开发语言·缓存·kotlin
爱好学习的青年人2 小时前
一文详解Go语言字符串
开发语言·后端·golang
浅川.252 小时前
xtuoj string
开发语言·c++·算法
咕白m6252 小时前
C# 合并多个PDF文档:高效解决方案
c#·.net
望获linux2 小时前
【实时Linux实战系列】实时系统的可观测性:Prometheus 与 Grafana 集成
大数据·linux·服务器·开发语言·网络·操作系统
加油吧zkf2 小时前
Python入门:从零开始的完整学习指南
开发语言·前端·python
xqlily3 小时前
Kotlin:现代编程语言的革新者
android·开发语言·kotlin