我正在尝试从ipconfig /all中查找网络接口名称。例如,如果ipconfig /all输出为:
Ethernet adapter Npcap Loopback Adapter:
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Npcap Loopback Adapter
Physical Address. . . . . . . . . : 01-00-4C-5F-5F-50
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
Autoconfiguration IPv4 Address. . : 169.254.183.10(Preferred)
Subnet Mask . . . . . . . . . . . : 255.255.0.0
Default Gateway . . . . . . . . . :
NetBIOS over Tcpip. . . . . . . . : Enabled 我想打印"Ethernet adapter Npcap Loopback Adapter“。这是我尝试过的:
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in interfaces)
{
richTextBox1.AppendText(adapter.Name);
}但它只打印“以太网”,而不是完整的东西。
发布于 2021-01-28 01:35:44
查看ipconfig的输出,我认为您看到的文本由两部分组成:
因此,您实际上只需要一个函数来将枚举转换为字符串,例如,它可以像这样简单:
private string GetNetworkTypeName(NetworkInterfaceType type) =>
type switch
{
NetworkInterfaceType.Ethernet => "Ethernet adapter",
NetworkInterfaceType.Wireless80211 => "Wireless LAN adapter",
//etc etc...
_ => "Other"
};并像这样构造名称:
var interfaceName = $"{GetNetworkTypeName(adapter.NetworkInterfaceType)} {adapter.Name}";https://stackoverflow.com/questions/65924121
复制相似问题