How to Check For Network Connection?
I'm a C# newbie so execuse my ignorance. I'm coding a client server application. One of the problems i've run into is that when the client is initially loaded and can't find a network connection to the server, i get this ugly .NET error message. I would like to be able to check for a network connection before loading the program. Please help!!!!
[368 byte] By [
jmulei2000] at [2007-11-19 18:51:54]

# 1 Re: How to Check For Network Connection?
Hi hi.
You should use a try-catch clause around an initial call to the server ( You could implement a phony Ping() method in the server that does nothing ).
On catching an exception you display a message box and exit the application, else you run it.
I'm not sure if this helps?
rgds,
sigrun
sigrun at 2007-11-9 11:19:14 >

# 2 Re: How to Check For Network Connection?
I'm a C# newbie so execuse my ignorance. I'm coding a client server application. One of the problems i've run into is that when the client is initially loaded and can't find a network connection to the server, i get this ugly .NET error message. I would like to be able to check for a network connection before loading the program. Please help!!!!
Add a reference to System.Management and call this function. //Check if network is connected
private bool IsNetworkConnected()
{
//a boolean variable to check if netwrok is connected
bool netConnected = false;
//get the adapters using WMI
System.Management.ManagementObjectSearcher netAdapters = new System.Management.ManagementObjectSearcher("SELECT NetConnectionStatus FROM Win32_NetworkAdapter");
//loop through Adapters to check if any of them is connected or not
foreach (System.Management.ManagementObject netAdapter in netAdapters.Get())
{
if (netAdapter["NetConnectionStatus"] != null)
{
if (Convert.ToInt32(netAdapter["NetConnectionStatus"]).Equals(2))
{
netConnected = true;
break;
}
}
}
netAdapters.Dispose();
return netConnected;
}The above function uses WMI to find the status of the Network Adapters.
Edit--
Have you tried using SystemInformation.Network. MSDN says that this will return true if Network is connected and false if it is disconnected.
if (SystemInformation.Network)
MessageBox.Show("Network Connected");
else
MessageBox.Show("Network not connected");
# 4 Re: How to Check For Network Connection?
You can also try to ping to the other computer. From previous trial and error I have tried, I found it to be very efficient.
I even implemented a class (very easy to use):
Ping in .NET 2003 (http://j1hammer.blogspot.com/2005/10/ping-in-net-2003.html)
public class Ping
{
private const string REQUEST_TIMED_OUT = "Request timed out.";
public static bool IsConnected(string ipAddress)
{
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.Arguments = ipAddress;
p.StartInfo.FileName=@"ping";
p.StartInfo.CreateNoWindow=true;
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
return AnalysePingOutput(output);
}
private static bool AnalysePingOutput(string output)
{
return !(output.IndexOf(REQUEST_TIMED_OUT)>0);
}
}