-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHostInfo.cs
111 lines (108 loc) · 4.69 KB
/
HostInfo.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System;
using System.Management;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
namespace UChat
{
/// <summary>
/// 获取本地主机各种信息。
/// </summary>
public class HostInfo
{
/// <summary>
/// 获取主机名
/// </summary>
/// <returns></returns>
public string HostName
{
get
{
string hostname = Dns.GetHostName();
return hostname;
}
}
/// <summary>
/// 获取主机 CPU ID。
/// </summary>
/// <returns></returns>
public string CPUID
{
get
{
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;
}
}
/// <summary>
/// 以 IPAddress 格式(原始格式)获取主机活动网络适配器的IPv4地址。
/// </summary>
/// <returns></returns>
public IPAddress IPv4Address
{
get
{
bool found = false;
IPAddress ipv4 = new IPAddress(0);
NetworkInterface[] fNetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in fNetworkInterfaces)
{
string fRegistryKey = "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\" + adapter.Id + "\\Connection";
Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(fRegistryKey, false);
if (rk != null)
{
// 区分 PnpInstanceID 注册表项
// 如果前面有 PCI 就是本机的真实网卡
string fPnpInstanceID = rk.GetValue("PnpInstanceID", "").ToString();
int fMediaSubType = Convert.ToInt32(rk.GetValue("MediaSubType", 0));
if (fPnpInstanceID.Length > 3 && fPnpInstanceID.Substring(0, 3) == "PCI")
{
if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet && adapter.OperationalStatus == OperationalStatus.Up)//以太网
{
IPInterfaceProperties iP = adapter.GetIPProperties();//获取网卡接口信息
UnicastIPAddressInformationCollection unicastIPs = iP.UnicastAddresses;//获取单播地址表
foreach (UnicastIPAddressInformation ipAddress in unicastIPs)
{
if (ipAddress.Address.AddressFamily == AddressFamily.InterNetwork)//如果是ipv4地址,ipv6是InterNetworkv6
{
ipv4 = ipAddress.Address;
found = true;
}
}
}
else if (adapter.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 && adapter.OperationalStatus == OperationalStatus.Up)//无线网络
{
IPInterfaceProperties iP = adapter.GetIPProperties();//获取网卡接口信息
UnicastIPAddressInformationCollection unicastIPs = iP.UnicastAddresses;//获取单播地址表
foreach (UnicastIPAddressInformation ipAddress in unicastIPs)
{
if (ipAddress.Address.AddressFamily == AddressFamily.InterNetwork)//如果是ipv4地址,ipv6是InterNetworkv6
{
ipv4 = ipAddress.Address;
found = true;
}
}
}
}
}
}
if (found == true)
{
return ipv4;
}
else
{
return IPAddress.Parse("0.0.0.0");
}
}
}
}
}