C# WinForm 判断本机是否联网
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
namespace AliWorkbenchProgram
{
class Class1
{
private bool IsInternetAvailable()
{
try
{
Dns.GetHostEntry("www.google.com"); //using System.Net;
return true;
}
catch (SocketException ex)
{
return false;
}
}
}
}
C#------如何获取本机IP地址
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
namespace AliWorkbenchProgram
{
class Class1
{
/// <summary>
/// 获取本地IP地址信息
/// </summary>
string GetAddressIP()
{
///获取本地的IP地址
string AddressIP = string.Empty;
foreach (IPAddress _IPAddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
{
if (_IPAddress.AddressFamily.ToString() == "InterNetwork")
{
AddressIP = _IPAddress.ToString();
}
}
return AddressIP;
}
}
}
![](https://img-blog.csdnimg.cn/20190129114232529.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3pnc2N3eGQ=,size_16,color_FFFFFF,t_70)