📄 networkinfo.cs
字号:
namespace Imps.Client.Utils.Information.Hardware
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
public class NetworkInfo
{
private static NetworkInfo _instance;
private string _network;
private NetworkInfo()
{
try
{
StringBuilder builder = new StringBuilder();
IPGlobalProperties iPGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
NetworkInterface[] allNetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
builder.AppendLine(string.Format("Interface information for {0}.{1} ", iPGlobalProperties.get_HostName(), iPGlobalProperties.get_DomainName()));
if ((allNetworkInterfaces == null) || (allNetworkInterfaces.Length < 1))
{
builder.AppendLine(" No network interfaces found.");
}
else
{
builder.AppendLine(string.Format(" Number of interfaces .................... : {0}", allNetworkInterfaces.Length));
foreach (NetworkInterface interface2 in allNetworkInterfaces)
{
IPInterfaceProperties adapterProperties = interface2.GetIPProperties();
builder.AppendLine();
builder.AppendLine(interface2.get_Description());
builder.AppendLine(string.Empty.PadLeft(interface2.get_Description().Length, '='));
builder.AppendLine(string.Format(" Interface name .......................... : {0}", interface2.get_Name()));
builder.AppendLine(string.Format(" Interface type .......................... : {0}", interface2.get_NetworkInterfaceType()));
builder.AppendLine(string.Format(" Physical Address ........................ : {0}", interface2.GetPhysicalAddress()));
builder.AppendLine(string.Format(" Operational status ...................... : {0}", interface2.get_OperationalStatus()));
string text = "";
if (interface2.Supports(0))
{
text = "IPv4";
}
if (interface2.Supports(1))
{
if (text.Length > 0)
{
text = text + " ";
}
text = text + "IPv6";
}
builder.AppendLine(string.Format(" IP version .............................. : {0}", text));
this.ShowIPAddresses(adapterProperties, builder);
if (interface2.get_NetworkInterfaceType() != 0x18)
{
builder.AppendLine(string.Format(" DNS suffix .............................. : {0}", adapterProperties.get_DnsSuffix()));
if (interface2.Supports(0))
{
IPv4InterfaceProperties properties3 = adapterProperties.GetIPv4Properties();
if (properties3 != null)
{
builder.AppendLine(string.Format(" MTU...................................... : {0}", properties3.get_Mtu()));
}
}
builder.AppendLine(string.Format(" DNS enabled ............................. : {0}", adapterProperties.get_IsDnsEnabled()));
builder.AppendLine(string.Format(" Dynamically configured DNS .............. : {0}", adapterProperties.get_IsDynamicDnsEnabled()));
builder.AppendLine(string.Format(" Receive Only ............................ : {0}", interface2.get_IsReceiveOnly()));
builder.AppendLine(string.Format(" Multicast ............................... : {0}", interface2.get_SupportsMulticast()));
}
}
this._network = builder.ToString();
}
}
catch
{
}
}
private void ShowIPAddresses(IPInterfaceProperties adapterProperties, StringBuilder builder)
{
IPAddressCollection addresss = adapterProperties.get_DnsAddresses();
if (addresss != null)
{
using (IEnumerator<IPAddress> enumerator = addresss.GetEnumerator())
{
while (enumerator.MoveNext())
{
IPAddress address = enumerator.get_Current();
builder.AppendLine(string.Format(" DNS Servers ............................. : {0}", address));
}
}
}
IPAddressInformationCollection informations = adapterProperties.get_AnycastAddresses();
if (informations != null)
{
using (IEnumerator<IPAddressInformation> enumerator2 = informations.GetEnumerator())
{
while (enumerator2.MoveNext())
{
IPAddressInformation information = enumerator2.get_Current();
builder.AppendLine(string.Format(" Anycast Address .......................... : {0} {1} {2}", information.get_Address(), information.get_IsTransient() ? "Transient" : "", information.get_IsDnsEligible() ? "DNS Eligible" : ""));
}
}
builder.AppendLine();
}
MulticastIPAddressInformationCollection informations2 = adapterProperties.get_MulticastAddresses();
if (informations2 != null)
{
using (IEnumerator<MulticastIPAddressInformation> enumerator3 = informations2.GetEnumerator())
{
while (enumerator3.MoveNext())
{
IPAddressInformation information2 = enumerator3.get_Current();
builder.AppendLine(string.Format(" Multicast Address ....................... : {0} {1} {2}", information2.get_Address(), information2.get_IsTransient() ? "Transient" : "", information2.get_IsDnsEligible() ? "DNS Eligible" : ""));
}
}
builder.AppendLine();
}
UnicastIPAddressInformationCollection informations3 = adapterProperties.get_UnicastAddresses();
if (informations3 != null)
{
string format = "dddd, MMMM dd, yyyy hh:mm:ss tt";
using (IEnumerator<UnicastIPAddressInformation> enumerator4 = informations3.GetEnumerator())
{
while (enumerator4.MoveNext())
{
UnicastIPAddressInformation information3 = enumerator4.get_Current();
builder.AppendLine(string.Format(" Unicast Address ......................... : {0}", information3.get_Address()));
builder.AppendLine(string.Format(" Prefix Origin ........................ : {0}", information3.get_PrefixOrigin()));
builder.AppendLine(string.Format(" Suffix Origin ........................ : {0}", information3.get_SuffixOrigin()));
builder.AppendLine(string.Format(" Duplicate Address Detection .......... : {0}", information3.get_DuplicateAddressDetectionState()));
DateTime time = DateTime.UtcNow + TimeSpan.FromSeconds((double) information3.get_AddressValidLifetime());
time = time.ToLocalTime();
builder.AppendLine(string.Format(" Valid Life Time ...................... : {0}", time.ToString(format, CultureInfo.CurrentCulture)));
time = DateTime.UtcNow + TimeSpan.FromSeconds((double) information3.get_AddressPreferredLifetime());
time = time.ToLocalTime();
builder.AppendLine(string.Format(" Preferred life time .................. : {0}", time.ToString(format, CultureInfo.CurrentCulture)));
time = DateTime.UtcNow + TimeSpan.FromSeconds((double) information3.get_DhcpLeaseLifetime());
time = time.ToLocalTime();
builder.AppendLine(string.Format(" DHCP Leased Life Time ................ : {0}", time.ToString(format, CultureInfo.CurrentCulture)));
}
}
builder.AppendLine();
}
}
public static NetworkInfo Instance
{
get
{
if (_instance == null)
{
_instance = new NetworkInfo();
}
return _instance;
}
}
public string Network
{
get
{
return this._network;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -