How to check your network connection state

Posted by: Clarity Blogs: ASP.NET, on 20 Jul 2009 | View original | Bookmarked: 0 time(s)

If you need to check the state of your network connection in C#, I know of 2 ways (that Im aware of) to do this:

 

1.  Steal from VB

You can use the VB My functions by importing the Microsoft.VisualBasic assembly into your project. 

In VB, you would normally use the following code:

My.Computer.Network.IsAvailable

In C#, you can use following code:

Microsoft.VisualBasic.Devices.Network network = new Microsoft.VisualBasic.Devices.Network();
Console.WriteLine(network.IsAvailable);

 

2. PInvoke

If you dont want to bring a VB reference into your C# project (which shouldnt really be a problem), you can always resort to PInvoking the native windows APIs.

Below is the code to code natively:

 

class Program
   {
       static void Main(string[] args)
       {
           Console.WriteLine(IsConnectedToInternet().ToString());
       }

       [DllImport("wininet.dll", SetLastError = true)]
       extern static bool InternetGetConnectedState(
          out InternetGetConnectedStateFlags Description, int ReservedValue);

       [Flags]
       enum InternetGetConnectedStateFlags
       {
           INTERNET_CONNECTION_MODEM = 0x01,
           INTERNET_CONNECTION_LAN = 0x02,
           INTERNET_CONNECTION_PROXY = 0x04,
           INTERNET_CONNECTION_RAS_INSTALLED = 0x10,
           INTERNET_CONNECTION_OFFLINE = 0x20,
           INTERNET_CONNECTION_CONFIGURED = 0x40
       }

       public static bool IsConnectedToInternet()
       {
           InternetGetConnectedStateFlags flags;
           var ret = InternetGetConnectedState(out flags, 0);
           Console.WriteLine(flags.ToString());
           return ret;
       }
   }

(I also updated pinvoke.net with this info)

Notes: 

1.  The Pinvoke method will give you an more detail by giving you an out parameter showing which connections are available to you

2.  These methods will only tell you if the network device is connected.  It wont tell you if you have can get out to the internet.

Advertisement
Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.
Category: VB.NET | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 2875 | Hits: 25

Similar Posts

  • Update Out Of Browser (OOB) Silverlight apps more
  • July's Toolbox Column Now Online more
  • RadioButton and CheckBox Menu Items with RadMenu for Silverlight more
  • NHibnerate : Handling the Special Cases more
  • Functional Programming Battles GOTOzilla more
  • Web.UI ASP.NET Grid: Synchronize Checkbox States with Row Selection more
  • When Do I Use Interfaces? more
  • TEC/DEC in Vegas Next Week more
  • Calling the Twitter API in C# more
  • Trying to debug a Silverlight project and is not breaking at breakpoints? more

News Categories

.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Data | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD