zoukankan      html  css  js  c++  java
  • 【C#】调用DOS命令

    1 public interface IRunConsole
    2     {
    3         void Run();
    4     }
    public abstract class RunConsole:IRunConsole
        {
            public abstract string[] Args { get; }
            public abstract string FileName { get; }//CMD
            public string result;
            public string error;
            public virtual int WaitExitedTime {
                get {
                    return 60000;
                }
            }
            #region IRunConsole 成员
    
            public void Run()
            {
                using(System.Diagnostics.Process process = new System.Diagnostics.Process()){
                    process.StartInfo = new System.Diagnostics.ProcessStartInfo(FileName)
                    {
                        RedirectStandardOutput=true,
                        RedirectStandardInput=true,
                        CreateNoWindow=true,
                        UseShellExecute=false,
                        RedirectStandardError = true
                    };
                    process.Start();
                    foreach (string arg in Args) {
                        process.StandardInput.WriteLine(arg);
                    }
                    
                    process.StandardInput.WriteLine(@"exit");
                    process.WaitForExit();
                    this.result = process.StandardOutput.ReadToEnd();
                    this.error = process.StandardError.ReadToEnd();
                    process.Close();
                    if (!string.IsNullOrEmpty(this.error)) {
                        throw new Exception(this.error);
                    }
                }
            }
    
            #endregion
        }
     1 public class RunDelDirectory : RunConsole
     2     {
     3         public string DirPath
     4         {
     5             get;
     6             set;
     7         }
     8         public override string[] Args
     9         {
    10             get {
    11                 return new string[]{
    12                   string.Format("rd /s/q {0}",this.DirPath)  
    13                 };
    14             }
    15         }
    16 
    17         public override string FileName
    18         {
    19             get { return "cmd"; }
    20         }
    21     }
  • 相关阅读:
    关于Class.getResource和ClassLoader.getResource的路径问题
    JavaScript高级程序设计(读书笔记)(一)
    CSS3常用选择器(二)
    CSS3常用选择器(一)
    JS中的for/in语句和arguments参数
    css绘制特殊图形基础
    css3 3d效果及动画学习
    圣杯布局和双飞翼布局
    css3弹性盒子模型
    css盒子模型
  • 原文地址:https://www.cnblogs.com/yomho/p/3147692.html
Copyright © 2011-2022 走看看