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;
 }
相关推荐
加号318 小时前
C# 基于MD5实现密码加密功能,附源码
开发语言·c#·密码加密
耿雨飞18 小时前
Python 后端开发技术博客专栏 | 第 05 篇 Python 数据模型与标准库精选 -- 写出 Pythonic 的代码
开发语言·python
执笔画流年呀18 小时前
计算机是如何⼯作的
linux·开发语言·python
weixin_5206498718 小时前
C#闭包知识点详解
开发语言·c#
东北甜妹18 小时前
Redis Cluster 操作命令
java·开发语言
花间相见18 小时前
【大模型微调与部署01】—— ms-swift-3.12入门:安装、快速上手
开发语言·ios·swift
techdashen18 小时前
Rust 正式成立 Types Team:类型系统终于有了专属团队
开发语言·后端·rust
jiayong2319 小时前
第 17 课:任务选择与批量操作
开发语言·前端·javascript·vue.js·学习
量子炒饭大师19 小时前
【C++11】RAII 义体加装指南 ——【包装器 与 异常】C++11中什么是包装器?有哪些包装器?C++常见异常有哪些?(附带完整代码讲解)
开发语言·c++·c++11·异常·包装器
telllong19 小时前
Python异步编程从入门到不懵:asyncio实战踩坑指南
开发语言·python