What is the best way to check for Internet connectivity using .NET?

后端 未结 27 2354
感动是毒
感动是毒 2020-11-22 07:41

What is the fastest and most efficient way to check for Internet connectivity in .NET?

27条回答
  •  滥情空心
    2020-11-22 08:21

    Try to avoid testing connections by catching the exception. because we really Expect that sometimes we may lose network connection.

     if (NetworkInterface.GetIsNetworkAvailable() &&
         new Ping().Send(new IPAddress(new byte[] { 8, 8, 8, 8 }),2000).Status == IPStatus.Success)
     //is online
     else
     //is offline
    

提交回复
热议问题