c# 返回 cmd 命令的结果
把结果作为一个整体返回:
1 public static string ExecuteCommandSync(object command) 2 { 3 try 4 { 5 // create the ProcessStartInfo using "cmd" as the program to be run, 6 // and "/c " as the parameters. 7 // Incidentally, /c tells cmd that we want it to execute the command that follows, 8 // and then exit. 9 System.Diagnostics.ProcessStartInfo procStartInfo = 10 new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command); 11 12 // The following commands are needed to redirect the standard output. 13 // This means that it will be redirected to the Process.StandardOutput StreamReader. 14 procStartInfo.RedirectStandardOutput = true; 15 procStartInfo.UseShellExecute = false; 16 // Do not create the black window. 17 procStartInfo.CreateNoWindow = true; 18 // Now we create a process, assign its ProcessStartInfo and start it 19 System.Diagnostics.Process proc = new System.Diagnostics.Process(); 20 proc.StartInfo = procStartInfo; 21 proc.Start(); 22 // Get the output into a string 23 string result = proc.StandardOutput.ReadToEnd(); 24 // Display the command output. 25 return result; 26 } 27 catch (Exception) 28 { 29 // Log the exception 30 return null; 31 } 32 }
将结果按行返回:
1 public static List<string> ExecuteCommand(object command) 2 { 3 try 4 { 5 List<string> list = new List<string>(); 6 // create the ProcessStartInfo using "cmd" as the program to be run, 7 // and "/c " as the parameters. 8 // Incidentally, /c tells cmd that we want it to execute the command that follows, 9 // and then exit. 10 System.Diagnostics.ProcessStartInfo procStartInfo = 11 new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command); 12 13 // The following commands are needed to redirect the standard output. 14 // This means that it will be redirected to the Process.StandardOutput StreamReader. 15 procStartInfo.RedirectStandardOutput = true; 16 procStartInfo.UseShellExecute = false; 17 // Do not create the black window. 18 procStartInfo.CreateNoWindow = true; 19 // Now we create a process, assign its ProcessStartInfo and start it 20 System.Diagnostics.Process proc = new System.Diagnostics.Process(); 21 proc.StartInfo = procStartInfo; 22 23 proc.Start(); 24 // Get the output into a string 25 //string result = proc.StandardOutput.ReadToEnd(); 26 StreamReader sr = proc.StandardOutput;//获取返回值 27 string line = ""; 28 int num = 1; 29 while ((line = sr.ReadLine()) != null) //按行读取 30 { 31 if (line != "") 32 { 33 //Console.WriteLine(line + " " + num++); 34 list.Add(line.Trim()); 35 } 36 } 37 //return result; 38 return list; 39 } 40 catch (Exception) 41 { 42 // Log the exception 43 return null; 44 } 45 }
参考:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架