Developing applications in C# that access resource on the internet is easy. This post shows four useful functions: checking if the PC is connected to a network, domain name to IP address lookup, ping a host and download a file.

Network Availability

You can check if there are any network connections using NetworkInterface.GetIsNetworkAvailable static method, found in the System.Net.NetworkInformation assembly. MSDN: "A network connection is considered to be available if any network interface is marked "up" and is not a loopback or tunnel interface." Here is how you could check for a network connection:

if (!System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
{
	MessageBox.Show("There is no network connection available.",
		"NetworkTools", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

Host Name Lookup

DNS Lookup

To lookup a host name's IP address or vice versa you could use the Dns.GetHostEntry static method in the System.Net assembly. It returns an IPHostEntry object that contains the host name and all the aliases and IP addesses associated with it. Here is an example that populates a ListView with all the IP addresses of a host name entered in a TextBox:

try
{
	IPHostEntry hostEntry = Dns.GetHostEntry(lookupHostTextBox.Text);

	lookupListView.Items.Clear();

	foreach (IPAddress ipAddress in hostEntry.AddressList)
		lookupListView.Items.Add(ipAddress.ToString());
}
catch (ArgumentNullException)
{
	MessageBox.Show("Please enter the host name or IP address to ping.");
}
catch (ArgumentOutOfRangeException)
{
	MessageBox.Show("The host name should be less than 127 characters");
}
catch (SocketException)
{
	MessageBox.Show("Could not resolve host name.");
}

hostTextBox.Focus();

Ping a Host

Ping

Pinging a host can be done using Send or SendAsync methods of the Ping object found in the System.Net.NetworkInformation assembly. Send pings the host and waits for a reply before returning to calling method. And returns a PingReply object that contains the ICMP echo's result and the status. SendAsync on the other hand pings the host asynchronously and returns immediately to the calling method. When SendAsync is finished the PingCompleted event occurs and the PingCompletedEventArgs parameter contains the details of the ping. The example below populates a ListView with the results of three pings to a host:

try
{
	int c = 3;
	IPAddress ipAddress = Dns.GetHostEntry(hostTextBox.Text).AddressList[0];

	resultsListView.Items.Clear();

	for (int i = 0; i < c; i++)
	{
		System.Net.NetworkInformation.Ping ping = 
			new System.Net.NetworkInformation.Ping();

		System.Net.NetworkInformation.PingReply pingReply = 
			ping.Send(ipAddress);

		ListViewItem result = new ListViewItem(pingReply.Address.ToString());
		result.SubItems.Add(pingReply.Buffer.Count().ToString());
		result.SubItems.Add(pingReply.RoundtripTime.ToString());
		result.SubItems.Add(pingReply.Options.Ttl.ToString());
		result.SubItems.Add(pingReply.Status.ToString());
		resultsListView.Items.Add(result);

		System.Threading.Thread.Sleep(100);
	}
}
catch (SocketException)
{
	MessageBox.Show("Could not resolve host name.");
}
catch (PingException ex)
{
	MessageBox.Show(ex.Message);
}
catch (ArgumentNullException)
{
	MessageBox.Show("Please enter the host name or IP address to ping.");
}

hostTextBox.Focus();

Download a File

DownloadFile

Downloading a file using the System.Net.WebClient can also be done synchronously and asynchronously. The DownloadFile method blocks while downloading the file. The asynchronous method DownloadFileAsync starts the download and when finished the DownloadFileCompleted event occurs.

System.Net.WebClient Client = new System.Net.WebClient();
Client.DownloadFile(fileUrlTextBox.Text, saveAsTextBox.Text);

The source code below contains all the examples above in one Visual Studio 2008 project.