using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ShareWifi.Utils { class Shell { /// /// 执行CMD语句 /// /// 要执行的CMD命令 public static string RunCmd(string cmd) { Process proc = new Process(); proc.StartInfo.CreateNoWindow = true; proc.StartInfo.FileName = "cmd.exe"; proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardError = true; proc.StartInfo.RedirectStandardInput = true; proc.StartInfo.RedirectStandardOutput = true; proc.Start(); proc.StandardInput.WriteLine(cmd); proc.StandardInput.WriteLine("exit"); proc.StandardInput.AutoFlush = true; string outStr = proc.StandardOutput.ReadToEnd(); proc.WaitForExit(); proc.Close(); return outStr; } /// /// 打开软件并执行命令 /// /// 软件路径加名称(.exe文件) /// 要执行的命令 public static void RunProgram(string programName, string cmd) { Process proc = new Process(); proc.StartInfo.CreateNoWindow = true; proc.StartInfo.FileName = programName; proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardError = true; proc.StartInfo.RedirectStandardInput = true; proc.StartInfo.RedirectStandardOutput = true; proc.Start(); if (cmd.Length != 0) { proc.StandardInput.WriteLine(cmd); } proc.Close(); } /// /// 打开软件 /// /// 软件路径加名称(.exe文件) public static void RunProgram(string programName) { RunProgram(programName, ""); } } }