马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

C#执行CMD命令公共方法

[复制链接]
查看3753 | 回复0 | 2014-10-7 09:24:27 | 显示全部楼层 |阅读模式
楼主转载,因为没时间出教程, 所以转载篇先,此代码楼主未测试

[C#] 纯文本查看 复制代码
using System;
using System.Diagnostics;

namespace Tool
{

    public class CMDHelper
    {
        public static string[] ExeCommand(string commandText)
        {

            Process 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.WaitForExit();

            string strOutput = p.StandardOutput.ReadToEnd();
            string strError = p.StandardError.ReadToEnd();//无错误则返回空字符串
            p.Close();
            return new string[] { strOutput, strError };
        }
    }
}




C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则