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;
 }
相关推荐
wuwu_q3 分钟前
用通俗易懂方式,详细讲讲 Kotlin Flow 中的 map 操作符
android·开发语言·kotlin
曼巴UE57 分钟前
UE5 C++ Slate 画曲线
开发语言·c++·ue5
向葭奔赴♡13 分钟前
Spring IOC/DI 与 MVC 从入门到实战
java·开发语言
minji...17 分钟前
C++ 面向对象三大特性之一---多态
开发语言·c++
散峰而望23 分钟前
基本魔法语言函数(一)(C语言)
c语言·开发语言·编辑器·github
lucky_syq1 小时前
Scala与Spark算子:大数据处理的黄金搭档
开发语言·spark·scala
封奚泽优1 小时前
使用Labelme进行图像标注
开发语言·python·labelme
wjs20241 小时前
C 标准库 - <ctype.h>
开发语言
wjs20241 小时前
AngularJS与SQL的集成使用指南
开发语言
七号练习生.c2 小时前
JavaScript基础入门
开发语言·javascript·ecmascript