How to check the Status of the Internet connection from windows application in C#.net
try this:
bool ConnectionExists()
{
try
{
System.Net.Sockets.TcpClient clnt=new System.Net.Sockets.TcpClient("www.google.com",80);
clnt.Close();
return true;
}
catch(System.Exception ex)
{
return false;
}
}
or this one
public static bool Test(string url)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "HEAD";
try {
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
resp.Close(); return true;
}
catch { return false; } }
or
In this tutorial, we'll create a class with a static function that returns true if connected and false if not, using our API function in private state.
Check this out :
using System ;
using System.Runtime ;
using System.Runtime.InteropServices ;
public class InternetCS
{
//Creating the extern function...
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState( int out Description, int ReservedValue ) ;
//Creating a function that uses the API function...
public static bool IsConnectedToInternet( )
{
int Desc ;
return InternetGetConnectedState( out Desc, 0 ) ;
}
}
No comments:
Post a Comment