? ??
? ? /// <summary>
? ? /// 使用 C# 自帶的類庫實現計算機信息獲取
? ? /// </summary>
? ? public class DefaultDeviceInfo?
? ? {
? ? ? ? public virtual string GetCpuId()
? ? ? ? {
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? string cpuInfo = " ";
? ? ? ? ? ? ? ? ManagementClass cimobject = new ManagementClass("Win32_Processor");
? ? ? ? ? ? ? ? ManagementObjectCollection moc = cimobject.GetInstances();
? ? ? ? ? ? ? ? foreach (ManagementObject mo in moc)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? return cpuInfo.ToString();
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception)
? ? ? ? ? ? {
? ? ? ? ? ? }
? ? ? ? ? ? return string.Empty;
? ? ? ? }
? ? ? ? public virtual string GetHDid()
? ? ? ? {
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? string HDid = " ";
? ? ? ? ? ? ? ? ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive");
? ? ? ? ? ? ? ? ManagementObjectCollection moc1 = cimobject1.GetInstances();
? ? ? ? ? ? ? ? foreach (ManagementObject mo in moc1)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? HDid = (string)mo.Properties["Model"].Value;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? return HDid.ToString();
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception)
? ? ? ? ? ? {
? ? ? ? ? ? }
? ? ? ? ? ? return string.Empty;
? ? ? ? }
? ? ? ? public virtual string GetIpv4()
? ? ? ? {
? ? ? ? ? ? string ip = GetIpv4("127.0.0.1");
? ? ? ? ? ? //如果根據127.0.0.1無法獲取ip地址,則嘗試根據主機名獲取
? ? ? ? ? ? if (ip == IPAddress.None.ToString())
? ? ? ? ? ? {
? ? ? ? ? ? ? ? ip = GetIpv4(null);
? ? ? ? ? ? }
? ? ? ? ? ? return ip;
? ? ? ? }
? ? ? ? public virtual string GetMac()
? ? ? ? {
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? List<string> macs = new List<string>();
? ? ? ? ? ? ? ? NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
? ? ? ? ? ? ? ? foreach (NetworkInterface ni in interfaces)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? if (ni.NetworkInterfaceType != NetworkInterfaceType.Ethernet)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? if (ni.GetPhysicalAddress().ToString() != "")
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? macs.Add(ni.GetPhysicalAddress().ToString());
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? //替補mac地址,當找不到以太網mac,則使用第一個mac
? ? ? ? ? ? ? ? var subs = macs.Count == 0 && interfaces.Length > 0
? ? ? ? ? ? ? ? ? ? ? interfaces[0].GetPhysicalAddress().ToString()
? ? ? ? ? ? ? ? ? ? : string.Empty;
? ? ? ? ? ? ? ? return macs.Count > 0 ? macs[0] : subs;
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception)
? ? ? ? ? ? {
? ? ? ? ? ? }
? ? ? ? ? ? return string.Empty;
? ? ? ? }
? ? ? ? public virtual string GetHostName()
? ? ? ? {
? ? ? ? ? ? return Dns.GetHostName();
? ? ? ? }
? ? ? ? #region 內部方法
? ? ? ? /// <summary>
? ? ? ? /// 字符串每隔多少位插入特定字符
? ? ? ? /// </summary>
? ? ? ? /// <param name="value"></param>
? ? ? ? /// <param name="separator"></param>
? ? ? ? /// <param name="separatorLen"></param>
? ? ? ? /// <returns></returns>
? ? ? ? protected static string StringInsertSeparator(string value, string separator, int separatorLen = 1)
? ? ? ? {
? ? ? ? ? ? if (string.IsNullOrWhiteSpace(value))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return value;
? ? ? ? ? ? }
? ? ? ? ? ? StringBuilder sbNewValue = new StringBuilder();
? ? ? ? ? ? for (int i = 0; i < value.ToArray().Count(); i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (i > 0 && i % separatorLen == 0)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? sbNewValue.Append(separator);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? sbNewValue.Append(value[i]);
? ? ? ? ? ? }
? ? ? ? ? ? return sbNewValue.ToString();
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// 根據指定的主機名獲取ip地址
? ? ? ? /// 如果主機名為空,則獲取系統主機名稱
? ? ? ? /// </summary>
? ? ? ? /// <param name="defHostName"></param>
? ? ? ? /// <returns></returns>
? ? ? ? protected string GetIpv4(string defHostName)
? ? ? ? {
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? /* NetworkInterface 實現方法
? ? ? ? ? ? ? ? // 獲得網絡接口,網卡,撥號器,適配器都會有一個網絡接口?
? ? ? ? ? ? ? ? NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
? ? ? ? ? ? ? ? foreach (NetworkInterface network in networkInterfaces)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? if (network.NetworkInterfaceType != NetworkInterfaceType.Ethernet)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? // 獲得當前網絡接口屬性
? ? ? ? ? ? ? ? ? ? IPInterfaceProperties properties = network.GetIPProperties();
? ? ? ? ? ? ? ? ? ? // 每個網絡接口可能會有多個IP地址?
? ? ? ? ? ? ? ? ? ? foreach (IPAddressInformation address in properties.UnicastAddresses)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? // 如果此IP不是ipv4,則進行下一次循環
? ? ? ? ? ? ? ? ? ? ? ? if (address.Address.AddressFamily != AddressFamily.InterNetwork)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? // 忽略127.0.0.1
? ? ? ? ? ? ? ? ? ? ? ? if (IPAddress.IsLoopback(address.Address))
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? return address.Address.ToString();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? */
? ? ? ? ? ? ? ? string hostName = defHostName;
? ? ? ? ? ? ? ? if (string.IsNullOrWhiteSpace(hostName))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? hostName = Dns.GetHostName();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? var ipList = Dns.GetHostEntry(hostName).AddressList;
? ? ? ? ? ? ? ? foreach (var address in ipList)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? // 如果此IP不是ipv4,則進行下一次循環
? ? ? ? ? ? ? ? ? ? if (address.AddressFamily != AddressFamily.InterNetwork)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? // 忽略127.0.0.1
? ? ? ? ? ? ? ? ? ? if (IPAddress.IsLoopback(address))
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? return address.ToString();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ??
? ? ? ? ? ? }
? ? ? ? ? ? return IPAddress.None.ToString(); //255.255.255.255?
? ? ? ? }
? ? ? ? #endregion
? ? }