C#之CMD

C#之CMD

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualBasic;
using System.Runtime.InteropServices;
using System.Net;
using System.Net.NetworkInformation;
using System.Text.RegularExpressions;
using System.Threading;

namespace KFC_v1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int Error;
        
        //CMD主命令
        private void cmd(string a)
        {
            Error = 0;
            Process myProcess = new Process();
            myProcess.StartInfo.FileName = "cmd.exe ";//DOS控制平台 
            myProcess.StartInfo.UseShellExecute = false;
            myProcess.StartInfo.CreateNoWindow = true;
            myProcess.StartInfo.RedirectStandardInput = true;
            myProcess.StartInfo.RedirectStandardOutput = true;
            myProcess.StartInfo.RedirectStandardError = true;
            myProcess.Start();
            StreamWriter sIn = myProcess.StandardInput;//标准输入流 
            sIn.AutoFlush = true;
            StreamReader sOut = myProcess.StandardOutput;//标准输出流 
            StreamReader sErr = myProcess.StandardError;//标准错误流 
            sIn.Write(a + System.Environment.NewLine);//DOS控制平台上的命令 
            sIn.Write("dir " + System.Environment.NewLine);//DOS控制平台上的命令 
            sIn.Write("exit " + System.Environment.NewLine);
            string s = sOut.ReadToEnd();//读取执行DOS命令后输出信息 
            string er = sErr.ReadToEnd();//读取执行DOS命令后错误信息 
            //MessageBox.Show(er);
            if ((er == null) || (er.Length != 0))
            {
                Error = 1;
                MessageBox.Show("CMD run error,please confirm the Adminstrator right or other issues\nCMD运行出错,请检查是否在具有管理员权限或者其他问题", "出错啦", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            //richTextBox1.AppendText(s);
            //richTextBox1.AppendText(er);
            if (myProcess.HasExited == false)
            {
                myProcess.Kill();
            }
            sIn.Close();
            sOut.Close();
            sErr.Close();
            myProcess.Close();
        }

 

转载于:https://www.cnblogs.com/Liangw/archive/2012/05/21/2559908.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/110404.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号