How to determine the network connection status of the system in C #

Source: Internet
Author: User

In C #, you can use multiple methods to determine the network connection status. For example, you can use Win32 APIs to determine the connection status, or you can directly ping a website to determine the connection status.

The following describes a method class that I write most to determine the network connection. It uses the combination of wine32 API functions and Ping. Send. The Code is as follows:

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. runtime. interopservices;
Using system. net. networkinformation;

Namespace testnetconnectwin32
{
Public class getinternetconstatus
{
Private const int internet_connection_modem = 1;
Private const int internet_connection_lan = 2;
[Dllimport ("wininet. dll")]
Private Static extern bool internetgetconnectedstate (
Ref int dwflag,
Int dwreserved
);

/// <Summary>
/// Determine the network connection status
/// </Summary>
/// <Returns>
/// Network status (1 --> not connected to the Internet; 2 --> enable the modem to access the Internet; 3 --> enable the NIC to access the Internet)
/// </Returns>
Public static int getnetconstatus (string strnetaddress)
{
Int inetstatus = 0;
System. int32 dwflag = new int ();
If (! Internetgetconnectedstate (ref dwflag, 0 ))
{
// Unable to connect to the Internet
Inetstatus = 1;
}
Else if (dwflag & internet_connection_modem )! = 0)
{
// Enable the Remote Desktop to access the Internet using the Remote Desktop modem. You need to determine whether you can log on to a specific website.
If (pingnetaddress (strnetaddress ))
{
// You can ping the specified URL. The network is OK.
Inetstatus = 2;
}
Else
{
// The specified URL cannot be pinged. The network is not OK.
Inetstatus = 4;
}
}

Else if (dwflag & internet_connection_lan )! = 0)
{
// If you use an Eni to access the Internet, you need to determine whether you can log on to a specific website.
If (pingnetaddress (strnetaddress ))
{
// You can ping the specified URL. The network is OK.
Inetstatus = 3;
}
Else
{
// The specified URL cannot be pinged. The network is not OK.
Inetstatus = 5;
}
}

Return inetstatus;
}

/// <Summary>
/// Ping the specific website to check whether the Ping is successful.
/// </Summary>
/// <Param name = "strnetadd"> </param>
/// <Returns> </returns>
Private Static bool pingnetaddress (string strnetadd)
{
Bool flage = false;
Ping = new Ping ();
Try
{
Pingreply Pr = ping. Send (strnetadd, 3000 );
If (PR. Status = ipstatus. timedout)
{
Flage = false;
}
If (PR. Status = ipstatus. Success)
{
Flage = true;
}
Else
{
Flage = false;
}
}
Catch
{
Flage = false;
}
Return flage;
}

}
}
// Judgment Method
Private void btnjudge_click (Object sender, eventargs E)
{
Int inetstatus = getinternetconstatus. getnetconstatus ("Baidu.com ");
If (inetstatus = 1)
{
Lblnetstatus. Text = "the network is not connected ";
}
Else if (inetstatus = 2)
{
Lblnetstatus. Text = "using a modem to access the Internet ";
}
Else if (inetstatus = 3)
{
Lblnetstatus. Text = "using network adapter ";
}
Else if (inetstatus = 4)
{
Lblnetstatus. Text = "Use the modem to access the Internet, but cannot connect to the specified network ";
}
Else if (inetstatus = 5)
{
Lblnetstatus. Text = "using the NIC to access the Internet, but cannot connect to the specified network ";
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.