C#模拟cmd


title: C#模拟cmd
tags: c#,cmd
author : Clown95


分享一段C#模拟终端的代码,具体用法直接看summary

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.IO;

{
    class ShellCmd
    {
    /// <summary>
    /// Cmd命令运行工具
    /// </summary>

        private string result;
        private string errinf;
        private Process process;
        private bool disposed;

        public ShellCmd() { disposed = false; }

        /// <summary>
        /// 是否忽略错误流(true可防卡死,但拿不到可能有的错误信息)
        /// </summary>
        public bool IngoreError { get; set; } 
        /// <summary>
        /// 获取命令运行后的结果(包括全部命令行)
        /// </summary>
        public string Result { get { return result; } }
        /// <summary>
        /// 获取命令运行后的错误信息(如果有)
        /// </summary>
        public string Errinf { get { return errinf; } }
        /// <summary>
        /// 是否释放了
        /// </summary>
        public bool Disposed { get { return disposed; } }
        /// <summary>
        /// 强制中止
        /// </summary>
        public void Dispose()
        {
            if (process != null && !process.HasExited)
            {
                process.Kill();
                process.Dispose();
                disposed = true;
            }
        }
        /// <summary>
        /// 运行cmd命令
        /// </summary>
        /// <param name="cmds">命令组</param>
        /// <param name="waitForExit">是否等待退出</param>
        /// <param name="needResult">是否需要获取输出结果</param>
        public void Run(string[] cmds, bool waitForExit=true, bool needResult=true)
        {
            result = errinf = string.Empty;

            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.StandardOutputEncoding = Encoding.UTF8;
         
            startInfo.FileName = "cmd.exe";
            startInfo.UseShellExecute = false;
            startInfo.RedirectStandardInput = true;
            startInfo.RedirectStandardOutput = true;
            startInfo.RedirectStandardError = IngoreError ? false : true;
            startInfo.CreateNoWindow = true;
            //startInfo.Verb = "runas";
            startInfo.WindowStyle = ProcessWindowStyle.Hidden;
            process = Process.Start(startInfo);
            StreamWriter input = process.StandardInput;
            StreamReader output = process.StandardOutput;
            StreamReader error = null;
            if(!IngoreError) error = process.StandardError;
            foreach (string cmd in cmds)
            {
                input.WriteLine(cmd);
                input.Flush();
            }
            input.Close();
            if (needResult) result = output.ReadToEnd();
            output.Close();
            if (!IngoreError)
            {
                errinf = error.ReadToEnd();
                error.Close();
            }
            if(waitForExit) process.WaitForExit();
            process.Close();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值