/// <summary>
/// Retrieves the connected state of the local system.
/// <para>
/// For more information go to
/// https://msdn.microsoft.com/en-us/library/windows/desktop/aa384702%28v=vs.85%29.aspx
/// </para>
/// </summary>
/// <param name="description">
/// C++ ( lpdwFlags [out]. Type: LPDWORD )<br />Pointer to a variable that receives the connection description. This
/// parameter may return a valid flag even when the function returns FALSE. This parameter can be one or more of the
/// following values.
/// <list type="table">
/// <listheader>
/// <term>Internet Connection State Possible Flags</term>
/// </listheader>
/// <item>
/// <term>INTERNET_CONNECTION_CONFIGURED (0x40)</term>
/// <description>
/// Local system has a valid connection to the Internet, but it might or might not be currently
/// connected.
/// </description>
/// </item>
/// <item>
/// <term>INTERNET_CONNECTION_LAN (0x02)</term>
/// <description>Local system uses a local area network to connect to the Internet.</description>
/// </item>
/// <item>
/// <term>INTERNET_CONNECTION_MODEM (0x01)</term>
/// <description>Local system uses a modem to connect to the Internet.</description>
/// </item>
/// <item>
/// <term>INTERNET_CONNECTION_MODEM_BUSY (0x08)</term>
/// <description>No longer used.</description>
/// </item>
/// <item>
/// <term>INTERNET_CONNECTION_OFFLINE (0x20)</term>
/// <description>Local system is in offline mode.</description>
/// </item>
/// <item>
/// <term>INTERNET_CONNECTION_PROXY (0x04)</term>
/// <description>Local system uses a proxy server to connect to the Internet.</description>
/// </item>
/// <item>
/// <term>INTERNET_RAS_INSTALLED (0x10)</term>
/// <description>Local system has RAS installed.</description>
/// </item>
/// </list>
/// </param>
/// <param name="reservedValue">C++ ( dwReserved [in].Type: )<br />This parameter is reserved and must be 0.</param>
/// <returns>
/// <c>true</c> if there is an active modem or a LAN Internet connection, <c>false</c> if there is no Internet
/// connection, or if all possible Internet connections are not currently active.
/// </returns>
/// <remarks>
/// A return value of TRUE from InternetGetConnectedState indicates that at least one connection to the Internet is
/// available. It does not guarantee that a connection to a specific host can be established. Applications should
/// always check for errors returned from API calls that connect to a server. InternetCheckConnection can be called to
/// determine if a connection to a specific destination can be established.
/// <br />A return value of TRUE indicates that either the modem connection is active, or a LAN connection is active
/// and a proxy is properly configured for the LAN.A return value of FALSE indicates that neither the modem nor the LAN
/// is connected.If FALSE is returned, the INTERNET_CONNECTION_CONFIGURED flag may be set to indicate that autodial is
/// configured to "always dial" but is not currently active.If autodial is not configured, the function returns FALSE.
/// <br />Like all other aspects of the WinINet API, this function cannot be safely called from within DllMain or the
/// constructors and destructors of global objects.
/// <br />Note WinINet does not support server implementations. In addition, it should not be used from a service. For
/// server implementations or services use Microsoft Windows HTTP Services (WinHTTP).
/// </remarks>
[DllImport("wininet.dll", SetLastError=true)]
extern static bool InternetGetConnectedState(out int lpdwFlags, int dwReserved);
[Flags]
enum ConnectionStates
{
Modem = 0x1,
LAN = 0x2,
Proxy = 0x4,
RasInstalled = 0x10,
Offline = 0x20,
Configured = 0x40,
}
Private Declare Function InternetGetConnectedState Lib "wininet.dll" _
(ByRef lpdwFlags As Int32, ByVal dwReserved As Int32) As Boolean
Private Enum ConnectionStates
Modem = &H1
LAN = &H2
Proxy = &H4
RasInstalled = &H10
Offline = &H20
Configured = &H40
End Enum
InternetGetConnectedStateFlags
Please add some!
C: http://support.microsoft.com/default.aspx?scid=kb;EN-US;242558
C#:
using System;
using System.Runtime.InteropServices;
namespace ConsoleApplication2
{
internal class Program
{
[DllImport("wininet.dll", SetLastError = true)]
private static extern bool InternetGetConnectedState(out int lpdwFlags, int dwReserved);
private static void Main(string[] args)
{
int flags;
bool isConnected = InternetGetConnectedState(out flags, 0);
Console.WriteLine(string.Format("Is connected :{0} Flags:{1}", isConnected, flags));
}
}
}
Function Get_InternetConnectedState(ByRef p_lngFlags As Long, Optional ByRef p_return_str As String = "") As Boolean
Dim lngFlags As Long
lngFlags = 0
Get_InternetConnectedState = False
If InternetGetConnectedState(lngFlags, 0) Then
'connected.
If lngFlags And ConnectionStates.LAN Then
'LAN connection.
p_return_str = "LAN connection."
ElseIf lngFlags And ConnectionStates.Modem Then
'Modem connection.
p_return_str = "Modem connection."
ElseIf lngFlags And ConnectionStates.Proxy Then
'Proxy connection.
p_return_str = "Proxy connection."
End If
Get_InternetConnectedState = True
Else
'not connected.
p_return_str = "Not connected."
Get_InternetConnectedState = False
End If
p_lngFlags = lngFlags
End Function
Try NetworkInterface.GetIsNetworkAvailable() in the the System.Net.NetworkInformation namespace. It returns true when a network is available and false otherwise. See also: http://msdn2.microsoft.com/en-us/library/system.net.networkinformation.networkinterface.getisnetworkavailable(VS.80).aspx. NOTE: this is all new in .NET 2.0!
Direct Link: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wininet/wininet/internetgetconnectedstate.asp