zoukankan      html  css  js  c++  java
  • cmd窗口获取数据,不间断ping

    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 System.Threading;

    namespace 测试另外一套代码
    {
        
    public partial class Form1 : Form
        {
            
    public Form1()
            {
                InitializeComponent();
            }
            System.IO.StreamWriter sw;  
    // 定义输出流 sw 作为Shell的标准输入,即命令 
            System.IO.StreamReader sr;  // 定义输出流 sr 作为Shell的标准输出,即正常结果
            System.IO.StreamReader err; // 定义输出流 err 作为Shell的错误输出,即出错结果
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            System.Diagnostics.ProcessStartInfo psI 
    = new System.Diagnostics.ProcessStartInfo(System.Environment.GetEnvironmentVariable("ComSpec"));


            
    delegate void chandle(string s);
            
    private void Form1_Load(object sender, EventArgs e)
            {

            }

            
    private void button1_Click(object sender, EventArgs e)
            {
                Thread tt 
    = new Thread(new ThreadStart(t));
                tt.Start();
            }
            
    private void t()
            {
                
    //p.StartInfo.WorkingDirectory = @"E:\TQ\";
                p.StartInfo.FileName = @"E:\TQ\tq.exe";
                
    //p.StartInfo.Arguments = @"I:\kn\m12\m12..ac3 I:\kn\m12\au2.wav";
                p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
                p.ErrorDataReceived 
    += new DataReceivedEventHandler(p_ErrorDataReceived);//这里多了这句
                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("ping www.china.com -t");
                
    //p.StartInfo.Arguments = " www.china.com -t";
                
    //p.BeginErrorReadLine(); //这里多了这句
                p.BeginOutputReadLine();
                p.WaitForExit();


            }

            
    void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
            {
                
    if (!string.IsNullOrEmpty(e.Data))
                {             
    // 做点工作
                    Invoke(new chandle(ct), e.Data);
                }
            }
            
    void p_ErrorDataReceived(object sender, DataReceivedEventArgs e)
            {
                
    if (!string.IsNullOrEmpty(e.Data))
                {             
    // 做点工作
                    Invoke(new chandle(ct), e.Data);
                }
            }
            
    private void ct(string s)
            {
                textBox1.AppendText(s.Trim() 
    + "\n");
                textBox1.SelectionStart 
    = textBox1.Text.Length - 1;
                Application.DoEvents();
            }

            
    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                
    //p.Dispose();
            }

            
    private void button2_Click(object sender, EventArgs e)
            {
                psI.UseShellExecute 
    = false;
                psI.RedirectStandardInput 
    = true;
                psI.RedirectStandardOutput 
    = true;
                psI.RedirectStandardError 
    = true;
                psI.CreateNoWindow 
    = true;
                p.StartInfo 
    = psI;
                Cursor 
    = System.Windows.Forms.Cursors.WaitCursor;

                p.Start();
                sw 
    = p.StandardInput;
                sr 
    = p.StandardOutput;
                err 
    = p.StandardError;
                sw.AutoFlush 
    = true;
                
    if (coboCommand.Text != "")
                {
                    sw.WriteLine(coboCommand.Text);
                }
                
    else
                {
                    sw.WriteLine(
    "echo 未输入命令");
                }
                sw.Close();
                textBox1.Text 
    = "输出结果为:" + sr.ReadToEnd();
                textBox1.Text 
    += "\n错误信息:\n" + err.ReadToEnd();
                Cursor 
    = System.Windows.Forms.Cursors.Default;
            }

            
    private void coboCommand_SelectedIndexChanged(object sender, EventArgs e)
            {

            }

            
    private void button3_Click(object sender, EventArgs e)
            {

            }
            
        }
    }
  • 相关阅读:
    arcgis server 中Web墨卡托投影与WGS-84坐标的转换
    jQuery的鼠标事件总结
    Aps.net中基于bootstrapt图片上传插件的应用
    动态添加div及对应的js、css文件
    jQuery时间格式插件-moment.js的使用
    arcgis地图服务之 identify 服务
    ASP.NET导出word实例
    ArcGIS字段计算器分割字段中的字符串
    正则表达式中的特殊字符
    arcgis for js开发之路径分析
  • 原文地址:https://www.cnblogs.com/welcomesay/p/2014266.html
Copyright © 2011-2022 走看看