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;
 }
相关推荐
hdsoft_huge1 小时前
Java & Spring Boot常见异常全解析:原因、危害、处理与防范
java·开发语言·spring boot
研华嵌入式1 小时前
如何在高通跃龙QCS6490 Arm架构上使用Windows 11 IoT企业版?
arm开发·windows·嵌入式硬件
风中的微尘1 小时前
39.网络流入门
开发语言·网络·c++·算法
未来之窗软件服务2 小时前
幽冥大陆(二)RDIFSDK 接口文档:布草洗涤厂高效运营的技术桥梁C#—东方仙盟
开发语言·c#·rdif·仙盟创梦ide·东方仙盟
小冯记录编程2 小时前
C++指针陷阱:高效背后的致命危险
开发语言·c++·visual studio
1uther2 小时前
Unity核心概念⑨:Screen
开发语言·游戏·unity·c#·游戏引擎
C_Liu_3 小时前
C++:类和对象(下)
开发语言·c++
coderxiaohan3 小时前
【C++】类和对象1
java·开发语言·c++
阿幸软件杂货间3 小时前
Office转PDF转换器v1.0.py
开发语言·pdf·c#
扯淡的闲人3 小时前
多语言编码Agent解决方案(5)-IntelliJ插件实现
开发语言·python