
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to check whether you are connected to Internet or not in C#?
There are many ways to check whether internet is connected to a machine in C# or not. Make use of System.Net namespace which provides common methods for sending data to and receiving data from a resource identified by a URI. The WebClient or HttpClient class provides common methods for sending data to or receiving data from any local, intranet, or Internet resource identified by a URI. Here in the below example we have used (OpenRead)Returns the data from a resource as a Stream.
Checks by hitting the url "http://google.com/generate_204" if success return true else false.
The below example runs in the loop and checks whether the internet is connected or not. If the Internet is connected return true or else return false.
Example
static void Main(string[] args){ var keepRetrying = true; while (keepRetrying){ if (IsConnectedToInternet()){ keepRetrying = false; System.Console.WriteLine("Connected"); } else { keepRetrying = true; System.Console.WriteLine("Not Connected"); } } } public static bool IsConnectedToInternet(){ try{ using (var client = new WebClient()) using (client.OpenRead("http://google.com/generate_204")) return true; } catch { } return false; }
Output
Connected
- Related Articles
- C++ Program to Check Whether a Graph is Strongly Connected or Not
- C++ Program to Check Whether it is Weakly Connected or Strongly Connected for a Directed Graph
- How to check whether a thread is alive or not in C#
- C# program to check whether two sequences are the same or not
- Program to check whether parentheses are balanced or not in Python
- Program to check whether given words are maintaining given pattern or not in C++
- How to check whether image is loaded or not?
- C# Program to check whether a directory exists or not
- Program to check whether two sentences are similar or not in Python
- Program to check whether elements frequencies are even or not in Python
- Check if a directed graph is connected or not in C++
- How to check whether a thread is a background thread or not in C#
- How to check whether the tree is symmetric or not using iterative in C#?
- How to check whether the tree is symmetric or not using recursion in C#?
- Golang Program to Check Whether Two Matrices are Equal or Not

Advertisements