المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Network connections detecting



C# Programming
06-30-2009, 04:52 AM
Hello,

VS 2008 SP1

I am using the code below to test whether the user is connected using either a wireless or LAN connection. i.e. that the cable is plugged in, or the wireless is switch off. The code works fine for this. However, if you can spot any potential problems with this or you know of a better way I would be interested to learn more.

However, the client would like us to check if the user is connected using a modem as well. As you can see from my source code I am looking for connection that start with either "Local Area Connection" or "Wireless Network Connection". This is ok.

However, the problem is that the modem name could be anything, as when the user sets up their modem connection using the 'new connection wizard' they can call this anything. So if my switch statement I don't know what to look for.

Any suggestions would be most helpfull,

// Checks if Network is either connected by LAN or Wireless
public bool IsNetworkConnected()
{
NetworkInterface[] networkCards = NetworkInterface.GetAllNetworkInterfaces();
bool connected = false;

// Loop through to find the one we want to check for connectivity.
// Connection can have different numbers appended so check that the
// network connections start with the conditions checked below.
foreach (NetworkInterface nc in networkCards)
{
// Check LAN
if (nc.Name.StartsWith("Local Area Connection"))
{
if (nc.OperationalStatus == OperationalStatus.Up)
{
connected = true;
}
}

// Check for Wireless
if (nc.Name.StartsWith("Wireless Network Connection"))
{
if (nc.OperationalStatus == OperationalStatus.Up)
{
connected = true;
}
}
}

return connected;
}