title: C# checking for active modem connection
body: <blockquote>
    <p><strong>Duplicate of</strong>: <a href="https://stackoverflow.com/questions/520347/c-how-do-i-check-for-a-network-connection">C# - How do I check for a network connection</a></p>
  </blockquote>
  
  <p>Hello,</p>
  
  <p>VS 2008 SP1</p>
  
  <p>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.</p>
  
  <p>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. </p>
  
  <p>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.</p>
  
  <p>Any suggestions would be most helpfull,</p>
  
  <pre><code>// 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;
          }
  </code></pre>