Click here to Skip to main content
15,512,747 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
atleast the code could also identify if it is connected and not connected anymore pleasee help because i want i to put into my project i have really no idea how to do it. thanks in advance =)
Posted

How about:Get the Available Network Connections Using WMI[^]

Best regards
Espen Harlinn
 
Share this answer
 
Comments
Orcun Iyigun 22-Feb-12 19:42pm    
Nice link. 5ed.
Espen Harlinn 23-Feb-12 11:15am    
Thank you, Orcun :)
Sergey Alexandrovich Kryukov 22-Feb-12 21:17pm    
Agree, a 5.
--SA
Espen Harlinn 23-Feb-12 11:16am    
Thank you Sergey :)
newbie011292 23-Feb-12 11:12am    
thanks
Use InternetGetConnectedState[^] function. And then use InternetCheckConnection[^] function. A sample code:

C#
using System; 
using System.Runtime; 
using System.Runtime.InteropServices;  
public class InternetCS 
{       
  [DllImport("wininet.dll")]     
  private extern static bool InternetGetConnectedState( out int Description, int ReservedValue );
  public static bool IsConnectedToInternet( )     
  {         
    int Desc ;         
    return InternetGetConnectedState( out Desc, 0 ) ;     
  } 
} 


-OR-


C#
WebClient web = new WebClient();  
try 

{  
  web.DownloadData("http://www.codeproject.com");  
}  
catch (WebException ex)  
{  
  Console.WriteLine("Could not connect");  
} 

If it fails you do not have conenction. But 1 st way is more reliable in my opinion.

Good luck,
OI
 
Share this answer
 
Comments
Espen Harlinn 22-Feb-12 19:45pm    
Excellent reply - my 5 :)
newbie011292 22-Feb-12 19:54pm    
sir i dont understand the 1st sorry im not that good in C#, is this the right way?i got errors too T_T.

public partial class Form1 : Form
{
[DllImport("wininet.dll")]

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);

public static bool IsConnectedToInternet()
{
int Desc;
return InternetGetConnectedState(out Desc, 0);
}
}
Sergey Alexandrovich Kryukov 22-Feb-12 21:20pm    
Good, my 5, but you should say the truth: second method is not reliable at all. Here is why: imagine CodeProject.com -- God forbid -- gets down for a short while...
--SA
Orcun Iyigun 23-Feb-12 11:40am    
Yeah that's right. I know it is not reliable that's why I also mentioned I would rather prefer the first way.
newbie011292 23-Feb-12 11:12am    
thanks
Check this Tip/Trick
Testing Internet Connectivity[^](Contains 5 different ways)
 
Share this answer
 
Comments
Espen Harlinn 28-Feb-12 14:24pm    
Nice find :)
C#
try
{
System.Net.IPHostEntry = System.Net.Dns.GetHostByName("www.google.com");
return true;
}
catch
{
return false; // host not reachable.
}
 
Share this answer
 
Comments
newbie011292 23-Feb-12 11:13am    
thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900