public class Console : IRun
{
public Console(){
this.TimeOut = ;
}
public string Result
{
get;
set;
}
public string Error
{
get;
set;
}
public int TimeOut
{
get;
set;
}
public string[] Cmds
{
get;
set;
}
#region IRun 成员 public void Run()
{
using (System.Diagnostics.Process process = new System.Diagnostics.Process())
{ process.StartInfo = new System.Diagnostics.ProcessStartInfo("cmd")
{
RedirectStandardOutput = true,
RedirectStandardInput = true,
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardError = true
}; process.Start();
System.Text.StringBuilder cmd = new StringBuilder();
foreach (string arg in Cmds)
{
if (string.IsNullOrEmpty(arg))
{
continue;
}
process.StandardInput.WriteLine(arg);
cmd.Append(arg);
}
process.StandardInput.WriteLine(@"exit");
try
{
process.WaitForExit(Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["CMDTimeOut"]));
}
catch
{
process.WaitForExit();
}
this.Result = process.StandardOutput.ReadToEnd();
this.Error = process.StandardError.ReadToEnd(); process.Close();
if (!string.IsNullOrEmpty(this.Error))
{
throw new Exception(string.Format("出错命令:\r\n{0}\r\n{1}", cmd.ToString(), this.Error));
}
}
} #endregion
}

C#执行cmd命令的更多相关文章

  1. JAVA之执行cmd命令

    感言在前:时隔好久没有更新博客园了,忙东忙西也没忙出个什么之所以然来.回首过去一两个月,只能用“疲倦”两个字来形容,而且是身心疲惫.每天11.12个小时的工作我都没觉得烦,但是总是想克服却又很难克服的 ...

  2. [转]Delphi执行CMD命令

    今天看到有人在问用代码执行CMD命令的问题,就总结一下用法,也算做个备忘. Delphi中,执行命令或者运行一个程序有2个函数,一个是winexec,一个是shellexecute.这两个大家应该都见 ...

  3. C# 执行CMD 命令

    /// <summary> /// 执行CMD 命令 /// </summary> /// <param name="strCommand">命 ...

  4. C# 执行CMD命令的方法

    /// <summary> /// 执行CMD命令 /// </summary> /// <param name="str"></para ...

  5. 如何使用Java执行cmd命令

    用JAVA代码实现执行CMD命令的方法! Runtime rt = Runtime.getRuntime(); Process p = rt.exec(String[] cmdarray);     ...

  6. java执行cmd命令并获取输出结果

    1.java执行cmd命令并获取输出结果 import java.io.BufferedReader; import java.io.InputStreamReader; import org.apa ...

  7. Java 调用并执行cmd命令

    cmd java 调用 执行 概要: Java 调用并执行cmd命令 Java | 复制 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2 ...

  8. Atitit.执行cmd 命令行 php

    Atitit.执行cmd 命令行 php 1. 执行cmd 命令行,调用系统命令的基础 1 1.1. 实际执行模式 1 1.2. 空格的问题 1 1.3. 中文路径的问题,程序文件读取编码设置 1 1 ...

  9. node执行cmd命令方法

    var cmd='tasklist';//获取 子进程模块的exec方法,用于执行cmd命令var exec = require('child_process').exec; //运行 定义的cmd命 ...

  10. java中执行cmd命令

    一.java执行cmd命令的三种方式:http://www.jb51.net/article/80829.htm 参考:https://www.cnblogs.com/zhufu9426/p/7928 ...

随机推荐

  1. 算法题C#

    几个大公司(IBM.MicroSoft and so on)面试经典数据结构与算法题C#解答 1.链表反转 我想到了两种比较简单的方法 第一种是需要开一个新的链表,将原链表的元素从后到前的插入到新链表 ...

  2. 【转】UiAutomator简要介绍

    原文地址:http://blog.csdn.net/g19920917/article/details/16131565 3.1.必备条件: 1.JDK    2.SDK(API高于15)    3. ...

  3. c# 官方命名规则

    官方的哦. https://msdn.microsoft.com/zh-cn/library/vstudio/ff926074(v=vs.110).aspx

  4. Ubuntu Ruby On Rails安装和配置

    在这篇文章中ubuntu通过rvm安装ruby和rails.步借鉴了官方网站和网上信息,这里给大家分享. 1. 安装mapapis公钥: gpg --keyserver hkp://keys.gnup ...

  5. Fedora20-Xfce音频视频解码器及解决无声音问题[已解决]

    安装rpmfusion源 http://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-20.noarch.rpm http:// ...

  6. Your build host version of Xamarin.IOS (release NO.)is too recent to work with the IOS designer

    Encounted such error in VS after I update the xamarin at Mac side.Here is the solution for u to refe ...

  7. Web前端框架与类库

    Web前端框架与类库的思考 说起前端框架,我也是醉了.现在去面试或者和同行聊天,动不动就这个框架碉堡了,那个框架好犀利. 当然不是贬低框架,只是有一种杀鸡焉用牛刀的感觉.网站技术是为业务而存在的,除此 ...

  8. ASP.NET DataTable的操作大全

    DataTable表示一个与内存有关的数据表,可以使用工具栏里面的控件拖放来创建和使用,也可以在编写程序过程中根据需要独立创建和使用,最常见的情况是作为DataSet的成员使用,在这种情况下就需要用在 ...

  9. phpcms 模板常用标签指南

    1.关于SEO <meta name="keywords" content="{$SEO[keyword]}"/> <meta name=&q ...

  10. 编写SQL的辅助工具

    原文:编写SQL的辅助工具 今天在同事的帮助下,下载了一个工具:ApexSQL edit,可能是我孤陋寡闻,不知道还有这样的好工具,它可以在我键入SQL时,帮助我提示表的名称和列名称.还可以帮助我格式 ...