C# 获取主机IP,Mac

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using System.Management.Instrumentation;
using System.Net;
namespace TaoBaoShuaId
{
     class TaoBaoShuaClass
    {
         #region 获得客户端外网IP地址
         ///   <summary>
        
///  获得客户端外网IP地址
        
///   </summary>
        
///   <returns> IP地址 </returns>
         public   string getClientInternetIPAddress()
        {
             string internetAddress =  "";
             try
            {
                 using (WebClient webClient =  new WebClient())
                {
                    internetAddress = webClient.DownloadString( " http://www.coridc.com/ip "); // 从外部网页获得IP地址
                    
// 判断IP是否合法
                     if (!System.Text.RegularExpressions.Regex.IsMatch(internetAddress,  " [0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3} "))
                    {
                        internetAddress = webClient.DownloadString( " http://fw.qq.com/ipaddress "); // 从腾讯提供的API中获得IP地址
                    }
                }
                 // 返回外网IP地址
                 return internetAddress;
            }
             catch
            {
                 return  " 外网IP地址:unknown ";
            }
             finally
            {

            }
        }
         #endregion

         #region 获取Mac
        
       
         ///   <summary>
        
///  获取Mac
        
///   </summary>
        
///   <returns></returns>
         public  string getMac()
        {
             string MacAddr =  null;
            ManagementObjectSearcher Query =  new ManagementObjectSearcher( " select * from Win32_NetworkAdapterConfiguration ");
            ManagementObjectCollection QueryCollection = Query.Get();
             foreach (ManagementObject mo  in QueryCollection)
            {
                 if (mo[ " IPEnabled "].ToString() ==  " True ")
                    MacAddr = mo[ " MacAddress "].ToString();
            }
             return MacAddr;
        }
         #endregion
    }
}

你可能感兴趣的:(mac)