zoukankan      html  css  js  c++  java
  • C#执行DOS命令(CMD命令) (转)

    在c#程序中,有时会用到调用cmd命令完成一些功能,于是在网上查到了如下方法,实现了 c#执行DOS命令,并返回结果。

    1.         //dosCommand Dos命令语句 
    2.         publicstring 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.         publicstaticstring 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.         } 

     其实还有一种我认为非常好的办法,调用VB的Shell函数,非常简单。

       引入 Microsoft.VisualBasic 开头的几个reference,然后调用下面的代码:

     Microsoft.VisualBasic.Interaction.Shell(path);

       这样就可以了

  • 相关阅读:
    IAR EWARM PRINTF/SCANF FORMATTER
    Windows Self Signed Driver
    Remove a Driver Package from the Driver Store
    CMSIS-DAP调试器
    CHM文件无法查看内容解决办法
    HRESULT 0x80131515 解决方法
    dmalloc 原文 翻译整理
    Linux错误代码
    Windows 错误代码
    调码王版本历史
  • 原文地址:https://www.cnblogs.com/nanfei/p/2620682.html
Copyright © 2011-2022 走看看