C # Run Program Echo

Source: Internet
Author: User

Description: Often have friends ask how to run a DOS command in C #, and intercepts the output, the output flow question, this question I previously implemented in the Java, because in C # has not encountered similar situation, in order to avoid each other people to ask each time again to demonstrate the situation, specially has made a simple example, Implement ping a Web site in WinForm and display the results of the ping in a text box.

Run the result diagram

Code generated by the form designer:
Namespace Runcmd
{
Partial class Cmdform
{
<summary>
The required designer variable.
</summary>
Private System.ComponentModel.IContainer components = null;

<summary>
Clean up all resources that are in use.
</summary>
<param name= "disposing" > if the managed resource should be freed, true; otherwise, false. </param>
protected override void Dispose (bool disposing)
{
if (disposing && (components!= null))
{
Components. Dispose ();
}
Base. Dispose (disposing);
}

Code generated #region the Windows forms Designer

<summary>
Designer supports the required methods-No
Use the Code Editor to modify the contents of this method.
</summary>
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 = "Label1";
this. Label1. Size = new System.Drawing.Size (29, 12);
this. Label1. TabIndex = 0;
this. Label1. Text = "ping";
//
Txtcommand
//
this. txtcommand.location = new System.Drawing.Point (41, 8);
this. Txtcommand.name = "Txtcommand";
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 = "Btnexecute";
this. btnexecute.size = new System.Drawing.Size (75, 23);
this. Btnexecute.tabindex = 2;
this. Btnexecute.text = "Execution";
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 = "Tbresult";
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 = "Cmdform";
this. Text = "Example of running command";
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;
}
}


Key parts of the 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
{
/* *
* Author: Zhou Gong
* BLOG:HTTP://BLOG.CSDN.NET/ZHOUFOXCN
* Date: 2007-07-07
*
* */
public partial class Cmdform:form
{
Public Cmdform ()
{
InitializeComponent ();
}

private void Btnexecute_click (object sender, EventArgs e)
{
Tbresult.text = "";
ProcessStartInfo start = new ProcessStartInfo ("Ping.exe"); Set Run command line file ask Ping.exe file, this file system will find itself
If it is a different EXE file, you may need to specify a detailed path, such as running WinRar.exe
Start. Arguments = Txtcommand.text; Set command parameters
Start. CreateNoWindow = true; Do not display DOS command line windows
Start. Redirectstandardoutput = true; //
Start. Redirectstandardinput = true; //
Start. UseShellExecute = false; Whether to specify the operating system shell process startup program
Process p = process.start (Start);
StreamReader reader = p.standardoutput; Intercept output stream
String line = reader. ReadLine (); Read one row at a time
while (! Reader. Endofstream)
{
Tbresult.appendtext (line + "");
line = reader. ReadLine ();
}
p.WaitForExit (); Wait for the program to finish executing the exit process
P.close (); Shutdown process
Reader. Close (); Close the stream
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.