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;
 }
相关推荐
telllong1 小时前
BeeWare:Python原生移动应用开发
开发语言·python
海海不瞌睡(捏捏王子)1 小时前
C#知识点概要
java·开发语言·1024程序员节
aini_lovee2 小时前
C# 实现邮件发送源码(支持附件)
开发语言·javascript·c#
_MyFavorite_2 小时前
JAVA重点基础、进阶知识及易错点总结(10)Map 接口(HashMap、LinkedHashMap、TreeMap)
java·开发语言
charlie1145141912 小时前
通用GUI编程技术——Win32 原生编程实战(十六)——Visual Studio 资源编辑器使用指南
开发语言·c++·ide·学习·gui·visual studio·win32
wheelmouse77883 小时前
网络排查基础与实战指南:Ping 与 Telnet
开发语言·网络·php
敲代码的嘎仔3 小时前
Java后端开发——真实面试汇总(持续更新)
java·开发语言·程序人生·面试·职场和发展·八股
迈巴赫车主3 小时前
蓝桥杯20560逃离高塔
java·开发语言·数据结构·算法·职场和发展·蓝桥杯
春日见4 小时前
E2E自驾规控30讲:导论
开发语言·驱动开发·git·matlab·计算机外设
wangchunting4 小时前
Jvm-垃圾收集器
java·开发语言·jvm