设为首页收藏本站language 语言切换
查看: 1559|回复: 0
收起左侧

C#环境下实现在WinForm中ping一个网站

[复制链接]
发表于 2010-2-25 10:51:50 | 显示全部楼层 |阅读模式
<p >C#中运行命令行截取输出流的例子本文介绍的是C#喜欢哦你好运行命令行截取输出流的一个例子,希望可以通过该例子使大家对C#命令行有更多了解。<p >此例旨在实现在WinForm中ping一个网站,并且将ping的结果显示在一个文本框中。<p >运行结果图:<p ><center><img  src="http://www.hh010.com/upload_files/article/244/9_nxcuii1851519.jpg"></center><p >窗体设计器产生的代码:<p ><CENTER><ccid_nobr><table width="400" border="1" cellspacing="0" cellpadding="2"  bordercolorlight = "black" bordercolordark = "#FFFFFF" align="center"><tr><td bgcolor="e6e6e6" class="code" ><pre><ccid_code>namespace RunCMD{    partial class CMDForm    {        /// &lt;summary&gt;        /// 必需的设计器变量。        /// &lt;/summary&gt;        private System.ComponentModel.IContainer components = null;        /// &lt;summary&gt;        /// 清理所有正在使用的资源。        /// &lt;/summary&gt;        /// &lt;param name=&quot;disposing&quot;&gt;如果应释放托管资源,为 true;否则为 false。&lt;/param&gt;        protected override void Dispose(bool disposing)        {            if (disposing &amp;&amp; (components != null))            {                components.Dispose();            }            base.Dispose(disposing);        }        #region Windows 窗体设计器生成的代码        /// &lt;summary&gt;        /// 设计器支持所需的方法 - 不要        /// 使用代码编辑器修改此方法的内容。        /// &lt;/summary&gt;        private void InitializeComponent()        {            this.label1 = new System.Windows.Forms.Label();            this.txtCommand = new System.Windows.Forms.TextBox();            this.btnExecute = new System.Windows.Forms.Button();            this.tbResult = new System.Windows.Forms.TextBox();            this.SuspendLayout();            //             // label1            //             this.label1.AutoSize = true;            this.label1.Location = new System.Drawing.Point(6, 11);            this.label1.Name = &quot;label1&quot;;            this.label1.Size = new System.Drawing.Size(29, 12);            this.label1.TabIndex = 0;            this.label1.Text = &quot;ping&quot;;            //             // txtCommand            //             this.txtCommand.Location = new System.Drawing.Point(41, 8);            this.txtCommand.Name = &quot;txtCommand&quot;;            this.txtCommand.Size = new System.Drawing.Size(269, 21);            this.txtCommand.TabIndex = 1;            //             // btnExecute            //             this.btnExecute.Location = new System.Drawing.Point(316, 6);            this.btnExecute.Name = &quot;btnExecute&quot;;            this.btnExecute.Size = new System.Drawing.Size(75, 23);            this.btnExecute.TabIndex = 2;            this.btnExecute.Text = &quot;执行&quot;;            this.btnExecute.UseVisualStyleBackColor = true;            this.btnExecute.Click += new System.EventHandler(this.btnExecute_Click);            //             // tbResult            //             this.tbResult.Location = new System.Drawing.Point(8, 47);            this.tbResult.Multiline = true;            this.tbResult.Name = &quot;tbResult&quot;;            this.tbResult.ScrollBars = System.Windows.Forms.ScrollBars.Both;            this.tbResult.Size = new System.Drawing.Size(383, 292);            this.tbResult.TabIndex = 3;            //             // CMDForm            //             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;            this.ClientSize = new System.Drawing.Size(403, 364);            this.Controls.Add(this.tbResult);            this.Controls.Add(this.btnExecute);            this.Controls.Add(this.txtCommand);            this.Controls.Add(this.label1);            this.Name = &quot;CMDForm&quot;;            this.Text = &quot;运行Command的例子&quot;;            this.ResumeLayout(false);            this.PerformLayout();        }        #endregion        private System.Windows.Forms.Label label1;        private System.Windows.Forms.TextBox txtCommand;        private System.Windows.Forms.Button btnExecute;        private System.Windows.Forms.TextBox tbResult;    }}</ccid_code></pre></td></tr></table></ccid_nobr></CENTER><p >关键部分代码:<p ><CENTER><ccid_nobr><table width="400" border="1" cellspacing="0" cellpadding="2"  bordercolorlight = "black" bordercolordark = "#FFFFFF" align="center"><tr><td bgcolor="e6e6e6" class="code" ><pre><ccid_code>using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Diagnostics;using System.IO;namespace RunCMD{    /**     * 作者:周公     * blog:http://blog.csdn.net/zhoufoxcn     * 日期:2007-07-07     *      * */    public partial class CMDForm : Form    {        public CMDForm()        {            InitializeComponent();        }        private void btnExecute_Click(object sender, EventArgs e)        {            tbResult.Text = &quot;&quot;;            ProcessStartInfo start = new ProcessStartInfo(&quoting.exe&quot;);//设置运行的命令行文件问ping.exe文件,这个文件系统会自己找到            //如果是其它exe文件,则有可能需要指定详细路径,如运行winRar.exe            start.Arguments = txtCommand.Text;//设置命令参数            start.CreateNoWindow = true;//不显示dos命令行窗口            start.RedirectStandardOutput = true;//            start.RedirectStandardInput = true;//            start.UseShellExecute = false;//是否指定操作系统外壳进程启动程序            Process p=Process.Start(start);            StreamReader reader = p.StandardOutput;//截取输出流            string line = reader.ReadLine();//每次读取一行            while (!reader.EndOfStream)            {                tbResult.AppendText(line+&quot; &quot;);                line = reader.ReadLine();            }            p.WaitForExit();//等待程序执行完退出进程            p.Close();//关闭进程            reader.Close();//关闭流        }    }}</ccid_code></pre></td></tr></table></ccid_nobr></CENTER><p ><p align="right">                                 <p align="center"></p></p>
您需要登录后才可以回帖 登录 | 论坛注册

本版积分规则

QQ|Archiver|手机版|小黑屋|sitemap|鸿鹄论坛 ( 京ICP备14027439号 )  

GMT+8, 2025-4-10 15:54 , Processed in 0.251120 second(s), 24 queries , Redis On.  

  Powered by Discuz!

  © 2001-2025 HH010.COM

快速回复 返回顶部 返回列表