从Winforms控制cmd.exe以运行程序

时间:2015-05-19 07:21:11

标签: .net winforms cmd command-prompt windows-console

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using Ivi.Visa.Interop;


namespace secureDataToolGUI
{
    public partial class Form1 : Form
    {
        String model;
        String serial;
        Process p;
        StreamWriter input;

        public Form1()
        {
            InitializeComponent();

            p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;

            p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);

            p.Start();
            input = p.StandardInput;
            p.BeginOutputReadLine();
            input.WriteLine("viFind32");
        }

        void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            update(e.Data + Environment.NewLine);
        }

        private void go_Click(object sender, EventArgs e)
        {
            String outPut = cmdTextBox.Text;
            //int location = outPut.IndexOf("PXI0");
            //String usbAddress = outPut.Substring(location, 4);
            int location = outPut.IndexOf("USB");
            String usbAddress = outPut.Substring(location, 33);
            input.WriteLine(content.Text);
            if (content.Text.Contains("\\"))
            {
                content.Text = "secureDataTool.exe " + usbAddress + " service snumber " + serial + " mnumber " + model;
                go.Text = "RUN";
            }
            else
            {
                content.Text = "";
                if (go.Text == "RUN")
                {
                    go.Text = "Confirm";
                }
                else { go.Text = "Load Program";
                RunCommand(usbAddress, ":SYST:PROD:SEAL");
                }
            }         
        }

        //申明delegate更新数据
        delegate void updateDelegate(string msg);
        void update(string msg)
        {
            if (this.InvokeRequired)
                Invoke(new updateDelegate(update), new object[] { msg });
            else
            {
                cmdTextBox.Text += msg;
                cmdTextBox.SelectionStart = cmdTextBox.Text.Length - 1;
                cmdTextBox.ScrollToCaret();
            }
            if (cmdTextBox.Text.EndsWith("continue> "))
            {
                //MessageBox.Show("Enter the serial number again in the Textbox and click Corfirm !");
                //input.WriteLine(serial);
                input.WriteLine(serial);
            }
        }
        //关闭GUI窗口
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            p.Close();
        }
        //打开文件浏览对话框
        private void locateFile_Click(object sender, EventArgs e)
        {
            this.openFileDialog1.ShowDialog();
        }
        //获取文件路径,将路径定位到文件所在位置
        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            string filepath = openFileDialog1.FileName;
            int fileposition = filepath.LastIndexOf("\\");
            content.Text = "cd " + filepath.Substring(0, fileposition);
        }

        private void modelSerial_TextChanged(object sender, EventArgs e)
        {
            String modelSerial = this.modelSerial.Text.Trim();
            modelSerial = modelSerial.Replace(" ", String.Empty);
            if (modelSerial.Length >= 19) {
                int modelLocation = 0;
                modelLocation = modelSerial.IndexOf("MY") != -1 ? modelSerial.IndexOf("MY") : modelSerial.IndexOf("US");
                serial = modelSerial.Substring(modelLocation);
                model = modelSerial.Substring(0, modelLocation);
            }
        }

        public String RunCommand(String address, string command)
        {
            String result = null;
            ResourceManager rm = new ResourceManager();
            var session = rm.Open(address);
            var msg = session as IMessage;
            if (msg != null)
            {
                try
                {
                    msg.WriteString(command);
                    result = msg.ReadString(2000);
                }
                catch (Exception)
                {

                }
            }
            session.Close();
            return result;
        }
    }
}

这是我控制cmd.exe的代码,现在我要做的是在检测输出"Enter the serial number again to continue> "时,程序可以自动输入序列如“MY33150879”来处理程序,怎么能我知道,我写了

if (cmdTextBox.Text.EndsWith("continue> "))
        {
            //MessageBox.Show("Enter the serial number again in the Textbox and click Corfirm !");
            //input.WriteLine(serial);
            input.WriteLine(serial);
        }

但这不起作用?

0 个答案:

没有答案