Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking network status in C#

Tags:

How do I check that I have an open network connection and can contact a specific ip address in c#? I have seen example in VB.Net but they all use the 'My' structure. Thank you.

like image 882
Brian Clark Avatar asked Nov 24 '08 14:11

Brian Clark


People also ask

How do I show network status?

Select the Start button, then type settings. Select Settings > Network & internet. The status of your network connection will appear at the top. Windows 10 lets you quickly check your network connection status.

What is a network status?

Network Status means an actual status of the network, including any Network Faults and/or Maintenance. Sample 1. Network Status means an actual status of the network, including any Network Faults and/or Maintenance. The Network Status can be checked on the LeaseWeb website.


2 Answers

If you just want to check if the network is up then use:

bool networkUp
    = System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();

To check a specific interface's status (or other info) use:

NetworkInterface[] networkCards
    = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();

To check the status of a remote computer then you'll have to connect to that computer (see other answers)

like image 72
Yona Avatar answered Oct 06 '22 00:10

Yona


If you want to monitor for changes in the status, use the System.Net.NetworkInformation.NetworkChange.NetworkAvailabilityChanged event:

NetworkChange.NetworkAvailabilityChanged 
    += new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);
_isNetworkOnline = NetworkInterface.GetIsNetworkAvailable();


// ...
void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
{
    _isNetworkOnline  = e.IsAvailable;
}
like image 31
CCrawford Avatar answered Oct 06 '22 01:10

CCrawford