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);

       这样就可以了

  • 相关阅读:
    安卓的sqlite增删改
    C#访问MySQL数据库(winform+EF)
    Sqlite在.NET下的使用和Sqlite数据库清理
    WPF小笔记-Popup拖动
    WPF自定义窗口最大化显示任务栏
    什么是Hash?什么是Hash算法或哈希函数?什么是map?什么是HashMap?HashMap的实现原理或者工作原理?HashMap是线程安全的吗?为什么?如何解决?
    字符串转换整数
    系统顺序图与顺序图区别,以及根据顺序图写代码
    设计领域模型有哪些难点?有哪些指导原则?
    斐波那契数列java实现
  • 原文地址:https://www.cnblogs.com/nanfei/p/2620682.html
Copyright © 2011-2022 走看看