C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

How to find out if the computer is connected to Internet ?


Posted Date: 19 Apr 2005    Resource Type: Articles    Category: .NET Framework
Author: Tony JohnMember Level: Diamond    
Rating: 1 out of 5Points: 10



The Microsoft Windows Internet (WinINet) application programming interface (API) can be used to work with various communication protocols including FTP and HTTP. The API 'InternetGetConnectedState' can be used to detect the internet connection and settings on a local computer.

About WinINet

Definition : The Windows Internet (WinINet) application programming interface (API) is a set of functions that enables applications to interact with Gopher, FTP, and HTTP protocols to access the vast resources on the Internet.

How to use the API InternetGetConnectedState to check if a computer is connected to internet ?

WinINet APIs are part of Windows platform SDK and there is no direct support for that in .NET. However, you can use platform invoke in .NET to call functions available in Windows DLLs. (Platform Invocation Services (PInvoke) allows managed code to call unmanaged functions that are implemented in a DLL.)

The API 'InternetGetConnectedState' is very simple to use - it return True if the local computer is connected to internet and return False if not connected.

This API takes a reference parameter, which will be populated with various connection settings values after calling the API. YOu can determine various settings like whether the connection is using Modem or LAN, is it connected through proxy etc.

Sample code:

Since we are calling a function in an external unmanaged DLL, we have to declare the function using DLLImport :

You must include the namspace 'System.Runtime.InteropServices' to use the platform invoke services.

using System.Runtime.InteropServices;

C# Syntax:

[DllImport("WININET", CharSet=CharSet.Auto)]
static extern bool InternetGetConnectedState( ref InternetConnectionState lpdwFlags, int dwReserved );

VB.NET Syntax:

Public Declare Function InternetGetConnectedState Lib "wininet" _
(ByRef dwFlags As Long, ByVal dwReserved As Long) As Long

Including the above line inside our class makes the API 'InternetGetConnectedState' available to our C# or VB.NET class. Now we can call InternetGetConnectedState just like any other method in our class.

Now, let us declare an enum which has some hexadecimal values used to represent various values returned by the API's reference parameter.

[Flags]
enum InternetConnectionState : int
{
INTERNET_CONNECTION_MODEM = 0x1,
INTERNET_CONNECTION_LAN = 0x2,
INTERNET_CONNECTION_PROXY = 0x4,
INTERNET_RAS_INSTALLED = 0x10,
INTERNET_CONNECTION_OFFLINE = 0x20,
INTERNET_CONNECTION_CONFIGURED = 0x40
}

The enum is used just for convenience. You can use the values represented by the enum directly in the code, instead of using the enum.

The following one line of code will now show whether the local computer is connected to internet or not.

InternetConnectionState flags = 0;
bool isConnected = InternetGetConnectedState(ref flags, 0)

See the sample code below how you can use the reference parameter 'flags' to determine other settings like whether Modem is used to connect to the internet, whether proxy is used etc.

bool isConfigured = (flags & InternetConnectionState.INTERNET_CONNECTION_CONFIGURED) != 0;
bool isOffline = (flags & InternetConnectionState.INTERNET_CONNECTION_OFFLINE) != 0;
bool isConnectedUsingModem = (flags & InternetConnectionState.INTERNET_CONNECTION_MODEM) != 0;
bool isConnectedUsingLAN = (flags & InternetConnectionState.INTERNET_CONNECTION_LAN) != 0;
bool isProxyUsed = (flags & InternetConnectionState.INTERNET_CONNECTION_PROXY) != 0;
bool isRasEnabled = (flags & InternetConnectionState.INTERNET_RAS_INSTALLED ) != 0;


Ref:
1. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wininet/wininet/internetgetconnectedstate.asp

2. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wininet/wininet/about_wininet.asp

3. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcwlkplatforminvoketutorial.asp




Responses

Author: Gyanendra Kumar Nayak    21 Apr 2005Member Level: Silver   Points : 0
Nice one. It really helps.


Author: Mr.Aung San Kyaw    02 Jun 2005Member Level: Bronze   Points : 0
I rewrite your source with console which is following:
*********************************************************
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace INetCSharp
{
public class Program
{
[DllImport("WININET", CharSet=CharSet.Auto)]
public static extern bool InternetGetConnectedState( ref InternetConnectionState lpdwFlags, int dwReserved );
public enum InternetConnectionState : int
{
INTERNET_CONNECTION_MODEM = 0x1,
INTERNET_CONNECTION_LAN = 0x2,
INTERNET_CONNECTION_PROXY = 0x4,
INTERNET_RAS_INSTALLED = 0x10,
INTERNET_CONNECTION_OFFLINE = 0x20,
INTERNET_CONNECTION_CONFIGURED = 0x40
}


static void Main(string[] args)
{
InternetConnectionState flags = 0;
bool isConfigured = (flags & InternetConnectionState.INTERNET_CONNECTION_CONFIGURED) != 0;
bool isOffline = (flags & InternetConnectionState.INTERNET_CONNECTION_OFFLINE) != 0;
bool isConnectedUsingModem = (flags & InternetConnectionState.INTERNET_CONNECTION_MODEM) != 0;
bool isConnectedUsingLAN = (flags & InternetConnectionState.INTERNET_CONNECTION_LAN) != 0;
bool isProxyUsed = (flags & InternetConnectionState.INTERNET_CONNECTION_PROXY) != 0;
bool isRasEnabled = (flags & InternetConnectionState.INTERNET_RAS_INSTALLED) != 0;
bool isConnected = InternetGetConnectedState(ref flags, 0);
//Connection
if (isConnected == true)
{
Console.WriteLine("Connected your computer with internet");
}
else
{
Console.WriteLine("your computer does not connect with internet");

}
//Configured
if (isConfigured == true)
{
Console.WriteLine("Configured");
}
else
{
Console.WriteLine("Not Configured");

}
//OFFLine
if (isOffline == true)
{
Console.WriteLine("OFF Line");
}
else
{
Console.WriteLine("Not OFF Line");

}
//Modem
if (isConnectedUsingModem == true)
{
Console.WriteLine("Use Modem");
}
else
{
Console.WriteLine("Not Use Modem");

}
//LAN
if (isConnectedUsingLAN == true)
{
Console.WriteLine("Use LAN");
}
else
{
Console.WriteLine("Not Use LAN");

}
//Proxy
if (isProxyUsed == true)
{
Console.WriteLine("Use Proxy");
}
else
{
Console.WriteLine("Not Use Proxy");

}
//Ras
if (isRasEnabled == true)
{
Console.WriteLine("Use Ras");
}
else
{
Console.WriteLine("Not Use Ras");

}

}
}
}
Your loving..,
Mr.Aung San Kyaw
mysolution@gmail.com


Author: Neetu Maheshwari    14 Jan 2008Member Level: Bronze   Points : 0
the code is hard to implement
when i use dis code.....i got lots of error....so please help me
dat how can i check whether internet is connected in local machine or not on a button click event
i m new to C#
plzzzzzz help me


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: What is the difference between string and System.String?
Previous Resource: Examples of how to Calculate Different VB.NET Dates
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use