Home > other >  How to check PC is connected with Internet or Intranet without IP address and Ping method in C#?
How to check PC is connected with Internet or Intranet without IP address and Ping method in C#?

Time:09-20

How can I check PC is Connected with Intranet or Internet without using Ping method and IP address in C#?

CodePudding user response:

For my use case, I needed to have a service up in a LAN network and check Network status from time to time. I eventually used this:

static readonly INetworkListManager NetworkListManager = new NetworkListManager();
// ...
while (NetworkListManager.IsConnected) {
  // do something...
}

// loop is over. no internet!
while (!NetworkListManager.IsConnected) {
  // wait for reconnection...
}

In my case that achieved what I needed, and with some testing it also worked on my WAN environment. But as always, there's no 100% correct assumption when it comes to interacting with other hardware. Incorrect result can happen, so put in as much as test you can.

CodePudding user response:

Here's something i used for a game of mine to detect if there is a network connection without it even touching or talking the router or other devices:

// Recommended for clean code
public static bool InternetConnection() => System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();

The code above is just the same as:

public static bool InternetConnection() { 
    return System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable(); 
}
  • Related