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;
            }
     
  • 相关阅读:
    今天碰到的angular 中的一个小坑
    mvc 防止客服端多次提交
    自定义通用Distinct去除重复数据的2中方式
    Sql 字符串操作类COALESCE
    SQL Server 性能优化
    Visual Studio Tip: Get Public Key Token for a Strong Named Assembly
    C#发送邮件
    Web打印组件jatoolsPrinter(转载)
    SQL SERVER 2005 同步复制技术(转)
    [Asp.net]常见word,excel,ppt,pdf在线预览方案(转)
  • 原文地址:https://www.cnblogs.com/xingyukun/p/1161870.html
Copyright © 2011-2022 走看看