Cc code controls the execution of DOS commands or external commands

From , 5 Years ago, written in C#, viewed 88 times.
URL https://pastebin.vip/view/597c7b40
  1.         //dosCommand Dos命令语句
  2.         public string Execute(string dosCommand)
  3.         {
  4.             return Execute(dosCommand, 10);
  5.         }
  6.         /// <summary>
  7.         /// 执行DOS命令,返回DOS命令的输出
  8.         /// </summary>
  9.         /// <param name="dosCommand">dos命令</param>
  10.         /// <param name="milliseconds">等待命令执行的时间(单位:毫秒),
  11.         /// 如果设定为0,则无限等待</param>
  12.         /// <returns>返回DOS命令的输出</returns>
  13.         public static string Execute(string command, int seconds)
  14.         {
  15.             string output = ""; //输出字符串
  16.             if (command != null && !command.Equals(""))
  17.             {
  18.                 Process process = new Process();//创建进程对象
  19.                 ProcessStartInfo startInfo = new ProcessStartInfo();
  20.                 startInfo.FileName = "cmd.exe";//设定需要执行的命令
  21.                 startInfo.Arguments = "/C " + command;//“/C”表示执行完命令后马上退出
  22.                 startInfo.UseShellExecute = false;//不使用系统外壳程序启动
  23.                 startInfo.RedirectStandardInput = false;//不重定向输入
  24.                 startInfo.RedirectStandardOutput = true; //重定向输出
  25.                 startInfo.CreateNoWindow = true;//不创建窗口
  26.                 process.StartInfo = startInfo;
  27.                 try
  28.                 {
  29.                     if (process.Start())//开始进程
  30.                     {
  31.                         if (seconds == 0)
  32.                         {
  33.                             process.WaitForExit();//这里无限等待进程结束
  34.                         }
  35.                         else
  36.                         {
  37.                             process.WaitForExit(seconds); //等待进程结束,等待时间为指定的毫秒
  38.                         }
  39.                         output = process.StandardOutput.ReadToEnd();//读取进程的输出
  40.                     }
  41.                 }
  42.                 catch
  43.                 {
  44.                 }
  45.                 finally
  46.                 {
  47.                     if (process != null)
  48.                         process.Close();
  49.                 }
  50.             }
  51.             return output;
  52.         }
  53. //csharp/6030

Reply to "Cc code controls the execution of DOS commands or external commands"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website