C # Use winnet to check network connection status

Source: Internet
Author: User

Note: the main body of this Code is composedXt0050 is provided, and 911 only sorts it out.

 


Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. runtime. interopservices;

Namespace pinyin
{
Public partial class form4: Form
{
// Define a constant
Private const long internet_connection_modem = 1; // local system uses a modem to connect to the Internet.
Private const long internet_connection_lan = 2; // local system uses a local area network to connect to the Internet.
Private const long internet_connection_proxy = 4; // local system uses a proxy server to connect to the Internet.
Private const long internet_connection_modem_busy = 8; // no longer used.
Private const long internet_connection_configured = 64; // local system has a valid connection to the Internet, but it might or might not be currently connected.
Private const long internet_connection_offline = 32; // local system is in offline mode.
Private const long internet_ras_installed = 16; // local system has Ras installed.
Public form4 ()
{
Initializecomponent ();
}
// Define (reference) API functions
[Dllimport ("wininet. dll")]
Public static extern bool internetgetconnectedstate (out long lpdwflags, long dwreserved );

Private void button#click (Object sender, eventargs E)
{
Long lfag;
String strconnectiondev = "";
If (internetgetconnectedstate (Out lfag, 0 ))
Strconnectiondev = "the network connection is normal! ";
Else
Strconnectiondev = "network connection unavailable! ";
If (lfag & internet_connection_offline)> 0)
Strconnectiondev + = "offline local system is in offline mode. ";
If (lfag & internet_connection_modem)> 0)
Strconnectiondev + = "modem: the local system uses a modem to connect to the Internet. ";
If (lfag & internet_connection_lan)> 0)
Strconnectiondev + = "LAN Local system uses a LAN to connect to the Internet. ";
If (lfag & internet_connection_proxy)> 0)
Strconnectiondev + = "a proxy ";
If (lfag & internet_connection_modem_busy)> 0)
Strconnectiondev + = "modem but modem is busy ";
MessageBox. Show (strconnectiondev );
}
}
}

Related Article

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.