Go to check your Internet connection with C #

Source: Internet
Author: User

Method 1:

How to check if your computer is connected to the Internet with C #. it's much more easier that other tutorials I 've seen in other sites. in deed, We're re going to use a simple API function internetgetconnectedstate, to return a Boolean variable.

This function takes two arguments:

The first one is an integer used with out keyword, that means that after calling the function, the variable will contain in an interger that describes the connection state (use of a modem, use of a proxy, offline mode ...). note that you must refer to www.msdn.com for more information about that.
The second one is a reserved variable that must be set to 0.

In this tutorial, we'll create a class with a static function that returns true if connected and false if not, using our API function in private State.

Check this out:

Using system;
Using system. runtime;
Using system. runtime. interopservices;

Public class internetcs
{

// Creating the extern function...
[Dllimport ("wininet. dll")]
Private extern static bool internetgetconnectedstate (INT out description, int reservedvalue );

// Creating a function that uses the API function...
Public static bool isconnectedtointernet ()
{

Int DESC;
Return internetgetconnectedstate (Out DESC, 0 );

}

}

Method 2: Add a class library (System. net. networkinformation)
# Region method 2
/// <Summary>
/// Check whether the IP address or domain name can be accessed using the TCP/IP protocol (using the ping command). True indicates that the Ping is successful, and false indicates that the ping fails.
/// </Summary>
/// <Param name = "stripordname"> enter a parameter, indicating the IP address or domain name. </param>
/// <Returns> </returns>
Public static bool pingipordomainname (string stripordname)
{
Try
{
Ping objpingsender = new Ping ();
Pingoptions objpinoptions = new pingoptions ();
Objpinoptions. dontfragment = true;
String data = "";
Byte [] buffer = encoding. utf8.getbytes (data );
Int inttime out = 120;
Pingreply objpinreply = objpingsender. Send (stripordname, inttimeout, buffer, objpinoptions );
String strinfo = objpinreply. Status. tostring ();
If (strinfo = "success ")
{
Return true;
}
Else
{
Return false;
}
}
Catch (exception)
{
Return false;
}
}
# Endregion

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.