可以执行多条命令,用“\r\n”分割


using System;
using System.Diagnostics;
namespace Tool
{
    public class CMDHelper
    {
        public static string[] ExeCommand(string commandText)
        {
            // System.Text.Encoding.RegisterProvider (System.Text.CodePagesEncodingProvider.Instance);
            // System.Console.OutputEncoding = System.Text.Encoding.GetEncoding("gb2312");
            using (var p = new Process())
            {

                p.StartInfo.FileName = Environment.GetEnvironmentVariable("ComSpec");
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                p.StartInfo.CreateNoWindow = true;
                p.Start();
                p.StandardInput.WriteLine(commandText);
                p.StandardInput.WriteLine("exit");
                p.StandardInput.Close();
                var outputTask = p.StandardOutput.ReadToEndAsync();
                strError  = p.StandardError.ReadToEnd();
                strOutput = outputTask.Result;
                p.WaitForExit();
                return new string[] { strOutput, strError };
            }
        }
    }
}

执行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. [标签] action的使用

    1.描述 This tag enables developers to call actions directly from a JSP page by specifying the action n ...

  2. jQuery获取JSON格式数据方法

    getJSON方法: jQuery.getJSON(url,data,success(data,status,xhr)) $("button").click(function(){ ...

  3. asp.net webform 局部发布更新

    一:关于webform编译 编译时会将每个aspx文件单独生成dll文件于bin目录下.也会将引用的dll存放于bin目录 二:对界面或者引用的dll(如BLL层,DAL层等)做了修改更新后在服务器只 ...

  4. SQL语句宝典

    1.前述: 将数据灵活运用于手掌心! link:1.经典SQL语句大全(cnblogs) 2.SQL教程(W3C)

  5. phpinfo.php

    ---恢复内容开始--- apache中的配置不对 查看httpd.conf文件中是否有: AddType ...... AddType application/x-httpd-php .php -- ...

  6. Mifare 1卡的存储结构

    存取控制指符合什么条件才能对卡片进行操作. S50和S70的块分为数据块和控制块,对数据块的操作有“读”.“写”.“加值”.“减值(含传输和存储)”四种,对控制块的操作只有“读”和“写”两种. S50 ...

  7. KeilMDK4.7a下载地址/中文乱码解决/自动关联设置

    推荐地址1,速度更快(确定为4.7a版本,且含注册机):1.http://www.mcuzone.com/software/keil/MDK470.RAR 2.http://kuai.xunlei.c ...

  8. handler.post(r)同一个线程的疑惑

    handler.post(r);是把r加到消息队列,但并未开辟新线程.等到消息被取出时才执行. package com.lei.handlethread; import android.os.Bund ...

  9. JVM探索(一)

    JVM测试的样例代码: import java.lang.management.ManagementFactory; /**  * @author zhailzh  *   * @Date 2015年 ...

  10. 【转】Derivation of the Normal Equation for linear regression

    I was going through the Coursera "Machine Learning" course, and in the section on multivar ...