C# WinForm 是否联网

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;
        }


    }
}

猜你喜欢

转载自blog.csdn.net/zgscwxd/article/details/86687461