12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ShareWifi.Utils
- {
- class Shell
- {
- /// <summary>
- /// 执行CMD语句
- /// </summary>
- /// <param name="cmd">要执行的CMD命令</param>
- 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;
- }
- /// <summary>
- /// 打开软件并执行命令
- /// </summary>
- /// <param name="programName">软件路径加名称(.exe文件)</param>
- /// <param name="cmd">要执行的命令</param>
- 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();
- }
- /// <summary>
- /// 打开软件
- /// </summary>
- /// <param name="programName">软件路径加名称(.exe文件)</param>
- public static void RunProgram(string programName)
- {
- RunProgram(programName, "");
- }
- }
- }
|