using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Management; namespace Utility
{
public static class LocalMachine
{
//获取IP地址: /// <summary>
/// 获取客户端内网IP地址
/// </summary>
/// <returns></returns>
public static string getClientLocalIPAddress()
{
string localIP = null;
try
{
IPHostEntry ipHost = System.Net.Dns.GetHostEntry(Dns.GetHostName());// Dns.Resolve(Dns.GetHostName()); ;
IPAddress ipaddress = ipHost.AddressList[];
localIP = ipaddress.ToString();
return "内网IP地址:" + localIP;
}
catch
{
return "内网IP地址:unknown";
}
finally
{ }
} /// <summary>
/// 获得客户端外网IP地址
/// </summary>
/// <returns>IP地址</returns>
public static 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地址
}
}
return "外网IP地址:" + internetAddress;
}
catch
{
return "外网IP地址:unknown";
}
finally
{ }
} public static string GetMacAddress()
{
try
{
//获取网卡硬件地址
string mac = "";
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if ((bool)mo["IPEnabled"] == true)
{
mac = mo["MacAddress"].ToString();
break;
}
}
moc = null;
mc = null;
return mac;
}
catch
{
return "unknow";
}
finally
{ }
}
public static string GetDiskID()
{
try
{
String HDid = "";
ManagementClass mc = new ManagementClass("Win32_DiskDrive");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
//HDid = (string)mo.Properties["Model"].ToString();
HDid = (String)mo.Properties["Model"].Value.ToString();
}
moc = null;
mc = null;
return HDid;
}
catch
{
return "unknow";
}
finally
{ }
}
//获取CPUid:
public static string getHost()
{
return System.Net.Dns.GetHostName();
} public static string GetCpuID()
{
try
{
//获取CPU序列号代码
string cpuInfo = "";//cpu序列号
ManagementClass mc = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
}
moc = null;
mc = null;
return cpuInfo;
}
catch
{
return "unknow";
}
finally
{ }
} }
}