• ¡Welcome to Square Theme!
  • This news are in header template.
  • Please ignore this message.
مهمان عزیز خوش‌آمدید. ورود عضــویت


امتیاز موضوع:
  • 13 رای - 2.92 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
Title: برسی وضعیت اتصال به اینترنت
حالت موضوعی
#1
سلام
چطوری میتونم بفهمم که الان به اینتر نت متصل هستم یا نه؟
 
پاسخ
#2
Check for internet connection in C#
برسی وضعیت اتصال به اینترنت c#
کد:
/// <summary>
/// Method used to check for internet connectivity by piging
/// varoaus websites and looking for the response.
/// </summary>
/// <returns>True if a ping succeeded, False if otherwise.</returns>
/// <remarks></remarks>
public bool isConnectionAvailable()
{
    //build a list of sites to ping, you can use your own
    string[] sitesList = { "www.google.com", "www.microsoft.com" , "www.psychocoder.net" };
    //create an instance of the System.Net.NetworkInformation Namespace
    Ping ping = new Ping();
    //Create an instance of the PingReply object from the same Namespace
    PingReply reply;
    //int variable to hold # of pings not successful
    int notReturned = 0;
     try
        {
         //start a loop that is the lentgh of th string array we
         //created above
            for (int i = 0; i <= sitesList.Length; i++)
            {
                //use the Send Method of the Ping object to send the
                //Ping request
                reply = ping.Send(sitesList[i], 10);
                //now we check the status, looking for,
                //of course a Success status
                if (reply.Status != IPStatus.Success)
                {
                    //now valid ping so increment
                    notReturned += 1;
                }
                //check to see if any pings came back
                if (notReturned == sitesList.Length)
                {
                    _success = false;
                    //comment this back in if you have your own excerption
                    //library you use for you applications (use you own
                    //exception names)
                    //throw new ConnectivityNotFoundException(@"There doest seem to be a network/internet connection.\r\n
                     //Please contact your system administrator");
                    //use this is if you don't your own custom exception library
                    throw new Exception(@"There doest seem to be a network/internet connection.\r\n
                    Please contact your system administrator");
                }
                else
                {
                    _success = true;
                }
            }
    }
    //comment this back in if you have your own excerption
    //library you use for you applications (use you own
    //exception names)
    //catch (ConnectivityNotFoundException ex)
    //use this line if you don't have your own custom exception
    //library
    catch (Exception ex)
    {
        _success = false;
        _returnMessage = ex.Message;
    }
    return _success;
}

//Example Useage
If(!(isConnectionAvailable))
{
   //then do something
}
{
   //then do something
}
گروه دور همی پارسی کدرز
https://t.me/joinchat/GxVRww3ykLynHFsdCvb7eg
 
پاسخ
  


موضوعات مشابه ...
موضوع نویسنده پاسخ بازدید آخرین ارسال
  اتصال به اکسس mahsa_18 1 3,657 06-11-2013، 01:10 AM
آخرین ارسال: Amin_Mansouri
  اتصال به sql server Ghoghnus 14 21,745 05-10-2012، 02:56 PM
آخرین ارسال: Ghoghnus
  سورس کد پاک کردن تمپ فایل اینترنت اکسپلور (سی شارپ) Amin_Mansouri 0 3,799 05-02-2012، 12:57 PM
آخرین ارسال: Amin_Mansouri
  برسی کبیسه بودن سال Ghoghnus 0 2,618 04-17-2012، 03:14 PM
آخرین ارسال: Ghoghnus
  اتصال به sql sqrverدر برنامه های تحت شبکه Ghoghnus 3 11,184 12-05-2011، 11:53 PM
آخرین ارسال: analyze
  به دست آوردن آدرسهای تایپ شده در اینترنت اکسپلورر Ghoghnus 0 2,758 10-31-2011، 02:53 PM
آخرین ارسال: Ghoghnus
  خالی کردن کش اینترنت اکسپلورر(emety cach folder) Ghoghnus 0 2,901 10-31-2011، 02:50 PM
آخرین ارسال: Ghoghnus

پرش به انجمن:


Browsing: 4 مهمان