Best way detect Network and Internet Connection

  • Thread starter Alhambra Eidos Desarrollo
  • Start date
A

Alhambra Eidos Desarrollo

Hi all mister,



Which is THE BEST WAY IN THE WORLD AROUND for:



1. detect Network



2. detect Internet Connection is working...



which is the best way in the world and good practice recommended by MVPs
Microsoft ?



Thanks in advanced..



code below...but which is the best code ???



NetworkInterface.GetIsNetworkAvailable()



private static int ERROR_SUCCESS = 0;
public static bool IsInternetConnected() {
long dwConnectionFlags = 0;
if (!InternetGetConnectedState(dwConnectionFlags, 0))
return false;

if(InternetAttemptConnect(0) != ERROR_SUCCESS)
return false;

return true;
}


[DllImport("wininet.dll", SetLastError=true)]
public static extern int InternetAttemptConnect(uint res);


[DllImport("wininet.dll", SetLastError=true)]
public static extern bool InternetGetConnectedState(long flags,long
reserved);


Correct translation below.


[DllImport("wininet.dll", SetLastError=true)]
public static extern bool InternetGetConnectedState(out int flags,int
reserved);



I found code in C#, how convert to VB.NET



HttpWebRequest req;
HttpWebResponse resp;
try
{
req = (HttpWebRequest)WebRequest.Create("http://www.google.com");
resp = (HttpWebResponse)req.GetResponse();

if(resp.StatusCode.ToString().Equals("OK"))
{
Console.WriteLine("its connected.");
}
else
{
Console.WriteLine("its not connected.");
}
}
catch(Exception exc)
{
Console.WriteLine("its not connected.");
}
 
T

Teme64

Alhambra said:
Hi all mister,



Which is THE BEST WAY IN THE WORLD AROUND for:



1. detect Network



2. detect Internet Connection is working...



which is the best way in the world and good practice recommended by MVPs
Microsoft ?



Thanks in advanced..



code below...but which is the best code ???



NetworkInterface.GetIsNetworkAvailable()



private static int ERROR_SUCCESS = 0;
public static bool IsInternetConnected() {
long dwConnectionFlags = 0;
if (!InternetGetConnectedState(dwConnectionFlags, 0))
return false;

if(InternetAttemptConnect(0) != ERROR_SUCCESS)
return false;

return true;
}


[DllImport("wininet.dll", SetLastError=true)]
public static extern int InternetAttemptConnect(uint res);


[DllImport("wininet.dll", SetLastError=true)]
public static extern bool InternetGetConnectedState(long flags,long
reserved);


Correct translation below.


[DllImport("wininet.dll", SetLastError=true)]
public static extern bool InternetGetConnectedState(out int flags,int
reserved);



I found code in C#, how convert to VB.NET



HttpWebRequest req;
HttpWebResponse resp;
try
{
req = (HttpWebRequest)WebRequest.Create("http://www.google.com");
resp = (HttpWebResponse)req.GetResponse();

if(resp.StatusCode.ToString().Equals("OK"))
{
Console.WriteLine("its connected.");
}
else
{
Console.WriteLine("its not connected.");
}
}
catch(Exception exc)
{
Console.WriteLine("its not connected.");
}
Converted with http://www.developerfusion.com/tools/convert/csharp-to-vb/

Dim req As HttpWebRequest
Dim resp As HttpWebResponse
Try
req = DirectCast(WebRequest.Create("http://www.google.com"), HttpWebRequest)
resp = DirectCast(req.GetResponse(), HttpWebResponse)

If resp.StatusCode.ToString().Equals("OK") Then
Console.WriteLine("its connected.")
Else
Console.WriteLine("its not connected.")
End If
Catch exc As Exception
Console.WriteLine("its not connected.")
End Try
 
A

Alhambra Eidos Desarrollo

And which is the best way to do this:

detect Network

detect Internet Connection

Thanks !!!
 
G

G.S.

And which is the best way to do this:

detect Network

detect Internet Connection

Thanks !!!

Sorry - I cannot answer your direct question, but I will be facing
your issue in a few weeks. My intention was to dig deep into how
Microsoft deal with it in the impelementation of their Disconnected
Service Agent Application block... There's plenty info about it on the
net.. here's an example:
http://www.codeplex.com/smartclient/Thread/View.aspx?ThreadId=15938
Maybe you can integrate that block instead...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top