1
//---------------------------------------------------------------------
2 // This file is part of the Microsoft .NET Framework SDK Code Samples.
4 // Copyright (C) Microsoft Corporation. All rights reserved.
6 //This source code is intended only as a supplement to Microsoft
7 //Development Tools and/or on-line documentation. See these other
8 //materials for detailed information regarding Microsoft code samples.
10 //THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
11 //KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
12 //IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
14 //---------------------------------------------------------------------
17 using System
.Collections
.Generic
;
18 using System
.ComponentModel
;
21 using System
.Windows
.Forms
;
22 using System
.Net
.NetworkInformation
;
23 using System
.Globalization
;
25 namespace Microsoft
.Samples
.PingClient
27 partial class PingClientForm
: Form
29 Ping pingClient
= new Ping();
31 public PingClientForm()
33 InitializeComponent();
34 pingClient
.PingCompleted
+=
35 new PingCompletedEventHandler(pingClient_PingCompleted
);
38 private void pingClient_PingCompleted(object sender
, PingCompletedEventArgs e
)
40 // Check to see if an error occurred. If no error, then display
41 // the address used and the ping time in milliseconds.
46 pingDetailsTextBox
.Text
+= " Ping cancelled. \r\n";
50 if (e
.Reply
.Status
== IPStatus
.Success
)
52 pingDetailsTextBox
.Text
+=
53 " " + e
.Reply
.Address
.ToString() + " " +
54 e
.Reply
.RoundtripTime
.ToString(
55 NumberFormatInfo
.CurrentInfo
) + "ms" + "\r\n";
59 pingDetailsTextBox
.Text
+=
60 " " + GetStatusString(e
.Reply
.Status
) + "\r\n";
66 // Otherwise display the error.
67 pingDetailsTextBox
.Text
+= " Ping error.\r\n";
69 "An error occurred while sending this ping. " +
70 e
.Error
.InnerException
.Message
);
72 sendButton
.Enabled
= true;
75 private string GetStatusString(IPStatus status
)
79 case IPStatus
.Success
:
81 case IPStatus
.DestinationHostUnreachable
:
82 return "Destination host unreachable.";
83 case IPStatus
.DestinationNetworkUnreachable
:
84 return "Destination network unreachable.";
85 case IPStatus
.DestinationPortUnreachable
:
86 return "Destination port unreachable.";
87 case IPStatus
.DestinationProtocolUnreachable
:
88 return "Destination protocol unreachable.";
89 case IPStatus
.PacketTooBig
:
90 return "Packet too big.";
91 case IPStatus
.TtlExpired
:
92 return "TTL expired.";
93 case IPStatus
.ParameterProblem
:
94 return "Parameter problem.";
95 case IPStatus
.SourceQuench
:
96 return "Source quench.";
97 case IPStatus
.TimedOut
:
100 return "Ping failed.";
104 private void sendButton_Click(object sender
, EventArgs e
)
106 // Select all the text in the address box.
107 addressTextBox
.SelectAll();
109 if (addressTextBox
.Text
.Length
!= 0)
111 // Disable the Send button.
112 sendButton
.Enabled
= false;
114 pingDetailsTextBox
.Text
+=
115 "Pinging " + addressTextBox
.Text
+ " . . .\r\n";
117 // Send ping request.
118 pingClient
.SendAsync(addressTextBox
.Text
, null);
122 MessageBox
.Show("Please enter an IP address or host name.");
127 private void cancelButton_Click(object sender
, EventArgs e
)
129 // Cancel any pending pings.
130 pingClient
.SendAsyncCancel();