zoukankan      html  css  js  c++  java
  • run a dos commands from C# code

    How to run a dos command from your C# code without showing a console window? Here is some code I copy from http://www.neeao.com/blog/article-3341.html
    I have used it in my project, it works.

    /// <summary>
            
    /// A method used to run a dos command hiddenly.
            
    /// </summary>
            
    /// <param name="dosCommand"></param>
            
    /// <param name="outtime"></param>
            
    /// <returns></returns>

            public static string Execute(string dosCommand, int outtime)
            
    {
                
    string output = "";
                
    if (dosCommand != null && dosCommand != "")
                
    {
                    Process process 
    = new Process();//create process object
                    ProcessStartInfo startinfo = new ProcessStartInfo();
                    startinfo.FileName 
    = "cmd.exe";//
                    
    //bellow is the setting to hidden the console window
                    startinfo.Arguments = "/c" + dosCommand;
                    startinfo.UseShellExecute 
    = false;
                    startinfo.RedirectStandardInput 
    = false;
                    startinfo.RedirectStandardOutput 
    = true;
                    startinfo.CreateNoWindow 
    = true;
                    process.StartInfo 
    = startinfo;

                    
    try
                    
    {
                        
    if (process.Start())
                        
    {
                            
    if (outtime == 0)
                            
    { process.WaitForExit(); }
                            
    else
                            
    { process.WaitForExit(outtime); }
                            output 
    = process.StandardOutput.ReadToEnd();
                        }

                    }

                    
    catch (SqlException e)
                    
    {
                        
    throw e;
                    }

                    
    catch (Exception e)
                    
    {
                        
    throw e;
                    }

                    
    finally
                    
    {
                        
    if (process != null)
                        
    { process.Close(); }
                    }

                }

                
    return output;
            }
     
  • 相关阅读:
    15个华丽的扁平风格的登录界面设计示例
    12款很酷的使用大头照的国外名片设计作品
    高清壁纸下载:15款精美的2014年元旦桌面壁纸
    Harp – 内置常用预处理器的静态 Web 服务器
    分享245款高质量的图标素材【免费下载】
    经典网页设计:关注用户体验的20个华丽网站
    使用 CSS3 实现超炫的 Loading(加载)动画效果
    你知道网页设计中最常用的15张图片素材吗?
    Koa – 更加强大的下一代 Node.js Web 框架
    Myth – 支持变量和数学函数的 CSS 预处理器
  • 原文地址:https://www.cnblogs.com/xingyukun/p/1161870.html
Copyright © 2011-2022 走看看