By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,165 Members | 1,916 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,165 IT Pros & Developers. It's quick & easy.

Best way detect Network and Internet Connection

P: n/a
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.");
}

--
http://www.alhambra-eidos.es/web2005/index.html
www.kiquenet.com/churrosoft
http://www.setbb.com/putainformatica...opic.php?p=843
www.trabajobasura.com/solusoft

Oct 29 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Alhambra Eidos Desarrollo wrote:
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.../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

--

Teme64 @ http://windevblog.blogspot.com
Oct 29 '08 #2

P: n/a


And which is the best way to do this:

detect Network

detect Internet Connection

Thanks !!!
Oct 30 '08 #3

P: n/a
On Oct 30, 3:31*am, Alhambra Eidos Desarrollo
<AlhambraEidosDesarro...@discussions.microsoft.com wrote:
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/...ThreadId=15938
Maybe you can integrate that block instead...
Oct 30 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.