usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Net.NetworkInformation;namespacepingexample{ Public Partial classForm1:form {#regionDesign/// <summary> ///the required designer variables. /// </summary> PrivateSystem.ComponentModel.IContainer components =NULL; /// <summary> ///clean up all the resources that are in use. /// </summary> /// <param name= "disposing" >true if the managed resource should be disposed, otherwise false. </param> protected Override voidDispose (BOOLdisposing) { if(Disposing && (Components! =NULL) ) {components. Dispose (); } Base. Dispose (disposing); } #regionCode generated by the Windows Forms Designer/// <summary> ///The Designer supports the required method-do not///Use the Code Editor to modify the contents of this method. /// </summary> Private voidInitializeComponent () { This. Label1 =NewSystem.Windows.Forms.Label (); This. Label2 =NewSystem.Windows.Forms.Label (); This. txt_ipaddress =NewSystem.Windows.Forms.TextBox (); This. Lst_pingresult =NewSystem.Windows.Forms.ListBox (); This. btn_startping =NewSystem.Windows.Forms.Button (); This. SuspendLayout (); // //Label1// This. Label1. AutoSize =true; This. Label1. Location =NewSystem.Drawing.Point ( -, -); This. Label1. Name ="Label1"; This. Label1. Size =NewSystem.Drawing.Size ( the, A); This. Label1. TabIndex =0; This. Label1. Text ="Remote Host IP:"; // //Label2// This. Label2. AutoSize =true; This. Label2. Location =NewSystem.Drawing.Point ( -, A); This. Label2. Name ="Label2"; This. Label2. Size =NewSystem.Drawing.Size ( $, A); This. Label2. TabIndex =1; This. Label2. Text ="Test Results:"; // //txt_ipaddress// This. txt_ipaddress.location =NewSystem.Drawing.Point (113, +); This. Txt_ipaddress.name ="txt_ipaddress"; This. txt_ipaddress.size =NewSystem.Drawing.Size (148, +); This. Txt_ipaddress.tabindex =2; // //Lst_pingresult// This. lst_pingresult.formattingenabled =true; This. Lst_pingresult.itemheight = A; This. lst_pingresult.location =NewSystem.Drawing.Point ( +, the); This. Lst_pingresult.name ="Lst_pingresult"; This. lst_pingresult.size =NewSystem.Drawing.Size (229,136); This. Lst_pingresult.tabindex =3; // //btn_startping// This. btn_startping.location =NewSystem.Drawing.Point ( the,239); This. Btn_startping.name ="btn_startping"; This. btn_startping.size =NewSystem.Drawing.Size ( the, at); This. Btn_startping.tabindex =4; This. Btn_startping.text ="Ping"; This. Btn_startping.usevisualstylebackcolor =true; This. Btn_startping.click + =NewSystem.EventHandler ( This. Btn_startping_click); // //Form1// This. Autoscaledimensions =NewSystem.Drawing.SizeF (6F, 12F); This. AutoScaleMode =System.Windows.Forms.AutoScaleMode.Font; This. ClientSize =NewSystem.Drawing.Size (292,284); This. Controls.Add ( This. btn_startping); This. Controls.Add ( This. Lst_pingresult); This. Controls.Add ( This. txt_ipaddress); This. Controls.Add ( This. Label2); This. Controls.Add ( This. Label1); This. Name ="Form1"; This. Text ="use of the ping class"; This. ResumeLayout (false); This. PerformLayout (); } #endregion PrivateSystem.Windows.Forms.Label Label1; PrivateSystem.Windows.Forms.Label Label2; PrivateSystem.Windows.Forms.TextBox txt_ipaddress; PrivateSystem.Windows.Forms.ListBox Lst_pingresult; PrivateSystem.Windows.Forms.Button btn_startping; #endregion PublicForm1 () {InitializeComponent (); } Private voidBtn_startping_click (Objectsender, EventArgs e) { This. Lst_PingResult.Items.Clear (); //Remote server IP stringIpstr =txt_IPAddress.Text.ToString (). Trim (); //Constructing a Ping instancePing Pingsender =NewPing (); //Ping Option SettingsPingOptions options =Newpingoptions (); Options. Dontfragment=true; //test Data stringdata =""; byte[] buffer =Encoding.ASCII.GetBytes (data); //setting the time-out period intTimeout = -; //Call the synchronous send method to send the data and save the returned result to the Pingreply instancepingreply reply =pingsender.send (IPSTR, timeout, buffer, options); if(reply. Status = =ipstatus.success) {LST_PINGRESULT.ITEMS.ADD ("host address of the reply:"+reply. Address.tostring ()); LST_PINGRESULT.ITEMS.ADD ("round-Trip time:"+reply. Roundtriptime); LST_PINGRESULT.ITEMS.ADD ("Time to Live (TTL):"+reply. OPTIONS.TTL); LST_PINGRESULT.ITEMS.ADD ("whether to control fragmentation of the packet:"+reply. Options.dontfragment); LST_PINGRESULT.ITEMS.ADD ("Buffer Size:"+reply. Buffer.length); } ElseLst_PingResult.Items.Add (reply. Status.tostring ()); } }}
Example of C # Ping, can be used to test the network, delay xx milliseconds