Use C # APIs to instantly determine the current network connection mode

Source: Internet
Author: User

Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Runtime. InteropServices;

Namespace MeshworkLinkMode
{
Class Program
{
Static void Main (string [] args)
{
Console. WriteLine ("using InternetGetConnectedState to determine the network connection mode ");
Console. WriteLine (Fun_InternetGetConnectedState ());
Console. WriteLine ("using IsNetworkAlive to determine the network connection mode ");
Console. WriteLine (Fun_IsNetworkAlive ());
Console. ReadLine ();
}

[DllImport ("wininet. dll")]
Private extern static bool InternetGetConnectedState (out int connectionDescription, int reservedValue );

[DllImport ("sensapi. dll")]
Private extern static bool IsNetworkAlive (out int connectionDescription );

Static string Fun_InternetGetConnectedState ()
{
Int INTERNET_CONNECTION_MODEM = 1;
Int INTERNET_CONNECTION_LAN = 2;
Int INTERNET_CONNECTION_PROXY = 4;
Int INTERNET_CONNECTION_MODEM_BUSY = 8;

String outPut = null;
Int flags; // method of surfing the internet
Bool m_bOnline = true; // whether it is online

M_bOnline = InternetGetConnectedState (out flags, 0 );
If (m_bOnline) // online
{
If (flags & INTERNET_CONNECTION_MODEM) = INTERNET_CONNECTION_MODEM)
{
OutPut = "online: dial-up Internet ";
}
If (flags & INTERNET_CONNECTION_LAN) = INTERNET_CONNECTION_LAN)
{
OutPut = "online: via LAN ";
}
If (flags & INTERNET_CONNECTION_PROXY) = INTERNET_CONNECTION_PROXY)
{
OutPut = "online: proxy ";
}
If (flags & INTERNET_CONNECTION_MODEM_BUSY) = INTERNET_CONNECTION_MODEM_BUSY)
{
OutPut = "MODEM is occupied by other non-INTERNET connections ";
}
}
Else
{
OutPut = "not online ";
}

Return outPut;
}

Static string Fun_IsNetworkAlive ()
{
Int NETWORK_ALIVE_LAN = 0;
Int NETWORK_ALIVE_WAN = 2;
Int NETWORK_ALIVE_AOL = 4;

String outPut = null;
Int flags; // method of surfing the internet
Bool m_bOnline = true; // whether it is online

M_bOnline = IsNetworkAlive (out flags );
If (m_bOnline) // online
{
If (flags & NETWORK_ALIVE_LAN) = NETWORK_ALIVE_LAN)
{
OutPut = "online: NETWORK_ALIVE_LAN ";
}
If (flags & NETWORK_ALIVE_WAN) = NETWORK_ALIVE_WAN)
{
OutPut = "online: NETWORK_ALIVE_WAN ";
}
If (flags & NETWORK_ALIVE_AOL) = NETWORK_ALIVE_AOL)
{
OutPut = "online: NETWORK_ALIVE_AOL ";
& Nb

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.