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;
 }
相关推荐
心无旁骛~17 分钟前
python多进程multiprocessing——spawn启动方式解析
开发语言·python
小石头 1008627 分钟前
【Java】String类(超级详细!!!)
java·开发语言·算法
conkl29 分钟前
Python中的鸭子类型:理解动态类型的力量
开发语言·python·动态·鸭子类型·动态类型规划
小小8程序员30 分钟前
swift的inout的用法
开发语言·ios·swift
极简之美35 分钟前
Mac 远程连接 Windows 简明教程(2025 实测版)
windows·macos
祈澈菇凉40 分钟前
Next.js 零基础开发博客后台管理系统教程(一):环境搭建与项目初始化
开发语言·javascript·ecmascript
wjs20241 小时前
Go 语言切片(Slice)
开发语言
muyouking111 小时前
Rust Slice 完全指南:从基础用法到 3D 场景实战
开发语言·3d·rust
电脑小管家1 小时前
蝰蛇鼠标驱动怎么安装?全型号驱动下载方法汇总
windows·驱动开发·计算机外设·电脑·游戏程序
Acrelhuang1 小时前
直击新能源电能质量痛点:安科瑞 APView500 在线监测装置应用方案
大数据·运维·开发语言·人工智能·物联网