C# 返回所有计算机网络接口集合
这是我想要返回我的机器网络接口集合的类。 我选择返回IEnumerable,但我不知道怎么做(我是一名新开发人员)。 或者梅比有更好的方法来建立我的班级C# 返回所有计算机网络接口集合,c#,C#,这是我想要返回我的机器网络接口集合的类。 我选择返回IEnumerable,但我不知道怎么做(我是一名新开发人员)。 或者梅比有更好的方法来建立我的班级 public class NetworkAdapter { string _name; string _id; string _description; string _ipAddress; string _gatewayIpAddress; string _speed;
public class NetworkAdapter
{
string _name;
string _id;
string _description;
string _ipAddress;
string _gatewayIpAddress;
string _speed;
string _networkInterfaceType;
string _macAddress;
public IEnumerable<NetworkAdapter> getAdapterInfo()
{
foreach (var adapter in NetworkInterface.GetAllNetworkInterfaces())
{
//fetch network configuration properties
var properties = adapter.GetIPProperties();
foreach (var uniCast in properties.UnicastAddresses)
{
//ignore loop-back addresses & IPv6 internet protocol family
if (!IPAddress.IsLoopback(uniCast.Address)
&& uniCast.Address.AddressFamily != AddressFamily.InterNetworkV6)
{
_name = adapter.Name;
_id = adapter.Id;
_description = adapter.Description;
_ipAddress = uniCast.Address.ToString();
_networkInterfaceType = adapter.NetworkInterfaceType.ToString();
_speed = adapter.Speed.ToString("#,##0");
_macAddress = adapter.GetPhysicalAddress().ToString();
var gatewayAddresses = adapter.GetIPProperties().GatewayAddresses;
foreach (var gatewayAddress in gatewayAddresses)
{
_gatewayIpAddress = gatewayAddress.Address.ToString();
}
}
}
}
yield return new NetworkAdapter att;
}
}
公共类网络适配器
{
字符串\u名称;
字符串_id;
字符串描述;
字符串地址;
字符串\u网关地址;
串速;
字符串networkInterfaceType;
字符串地址;
公共IEnumerable getAdapterInfo()
{
foreach(NetworkInterface.GetAllNetworkInterfaces()中的var适配器)
{
//获取网络配置属性
var properties=adapter.GetIPProperties();
foreach(在properties.unicastaddress中的var单播)
{
//忽略环回地址和IPv6 internet协议系列
if(!IPAddress.IsLoopback(单播.Address)
&&uniCast.Address.AddressFamily!=AddressFamily.InterNetworkV6)
{
_name=adapter.name;
_id=adapter.id;
_description=适配器。description;
_ipAddress=uniCast.Address.ToString();
_networkInterfaceType=适配器.networkInterfaceType.ToString();
_speed=适配器.speed.ToString(“#,#0”);
_macAddress=adapter.GetPhysicalAddress().ToString();
var gatewayAddresses=adapter.GetIPProperties().gatewayAddresses;
foreach(网关地址中的var网关地址)
{
_gatewayIpAddress=gatewayAddress.Address.ToString();
}
}
}
}
返回新的网络适配器att;
}
}
您需要创建NetworkAdapter
的对象,并在yield
返回中使用该对象。我将私有成员更改为公共成员,以使消费类可以访问它们
public class NetworkAdapter
{
public string Name {get; set;}
public string Id {get; set;}
public string Description {get; set;}
public string IpAddress {get; set;}
public string GatewayIpAddress {get; set;}
public string Speed {get; set;}
public string NetworkInterfaceType {get; set;}
public string MacAddress {get; set;}
public IEnumerable<NetworkAdapter> getAdapterInfo()
{
NetworkAdapter na = null;
foreach (NetworkInterface adapter in NetworkInterface.GetAllNetworkInterfaces())
{
IPInterfaceProperties properties = adapter.GetIPProperties(); //fetch network configuration properties
foreach (IPAddressInformation uniCast in properties.UnicastAddresses)
{
if (!IPAddress.IsLoopback(uniCast.Address) && uniCast.Address.AddressFamily != AddressFamily.InterNetworkV6) //ignore loop-back addresses & IPv6 internet protocol family
{
na = new NetworkAdapter{
Name = adapter.Name;
Id= adapter.Id;
Description= adapter.Description;
IpAddress= uniCast.Address.ToString();
GatewayIpAddress= adapter.NetworkInterfaceType.ToString();
Speed= adapter.Speed.ToString("#,##0");
NetworkInterfaceType = adapter.GetPhysicalAddress().ToString();
};
foreach (GatewayIPAddressInformation gatewayAddress in adapter.GetIPProperties().GatewayAddresses)
{
na.GatewayIpAddress = gatewayAddress.Address.ToString();
}
}
}
yield return na ;
}
}
}
公共类网络适配器
{
公共字符串名称{get;set;}
公共字符串Id{get;set;}
公共字符串说明{get;set;}
公共字符串IpAddress{get;set;}
公共字符串网关IP地址{get;set;}
公共字符串速度{get;set;}
公共字符串NetworkInterfaceType{get;set;}
公共字符串MacAddress{get;set;}
公共IEnumerable getAdapterInfo()
{
网络适配器na=null;
foreach(NetworkInterface.GetAllNetworkInterfaces()中的NetworkInterface适配器)
{
IPInterfaceProperties properties=adapter.GetIPProperties();//获取网络配置属性
foreach(IPAddressInformation在properties.UnicastAddresses中单播)
{
if(!IPAddress.IsLoopback(uniCast.Address)&&uniCast.Address.AddressFamily!=AddressFamily.InterNetworkV6)//忽略回送地址和IPv6 internet协议族
{
na=新网络适配器{
Name=adapter.Name;
Id=adapter.Id;
Description=适配器。Description;
IpAddress=uniCast.Address.ToString();
GatewayIpAddress=adapter.NetworkInterfaceType.ToString();
Speed=适配器.Speed.ToString(“#,#0”);
NetworkInterfaceType=adapter.GetPhysicalAddress().ToString();
};
foreach(适配器中的GatewayIPAddress信息gatewayAddress.GetIPProperties().GatewayAddresses)
{
na.GatewayIpAddress=gatewayAddress.Address.ToString();
}
}
}
收益率;
}
}
}
您需要创建NetworkAdapter
的对象,并在yield
返回中使用该对象。我将私有成员更改为公共成员,以使消费类可以访问它们
public class NetworkAdapter
{
public string Name {get; set;}
public string Id {get; set;}
public string Description {get; set;}
public string IpAddress {get; set;}
public string GatewayIpAddress {get; set;}
public string Speed {get; set;}
public string NetworkInterfaceType {get; set;}
public string MacAddress {get; set;}
public IEnumerable<NetworkAdapter> getAdapterInfo()
{
NetworkAdapter na = null;
foreach (NetworkInterface adapter in NetworkInterface.GetAllNetworkInterfaces())
{
IPInterfaceProperties properties = adapter.GetIPProperties(); //fetch network configuration properties
foreach (IPAddressInformation uniCast in properties.UnicastAddresses)
{
if (!IPAddress.IsLoopback(uniCast.Address) && uniCast.Address.AddressFamily != AddressFamily.InterNetworkV6) //ignore loop-back addresses & IPv6 internet protocol family
{
na = new NetworkAdapter{
Name = adapter.Name;
Id= adapter.Id;
Description= adapter.Description;
IpAddress= uniCast.Address.ToString();
GatewayIpAddress= adapter.NetworkInterfaceType.ToString();
Speed= adapter.Speed.ToString("#,##0");
NetworkInterfaceType = adapter.GetPhysicalAddress().ToString();
};
foreach (GatewayIPAddressInformation gatewayAddress in adapter.GetIPProperties().GatewayAddresses)
{
na.GatewayIpAddress = gatewayAddress.Address.ToString();
}
}
}
yield return na ;
}
}
}
公共类网络适配器
{
公共字符串名称{get;set;}
公共字符串Id{get;set;}
公共字符串说明{get;set;}
公共字符串IpAddress{get;set;}
公共字符串网关IP地址{get;set;}
公共字符串速度{get;set;}
公共字符串NetworkInterfaceType{get;set;}
公共字符串MacAddress{get;set;}
公共IEnumerable getAdapterInfo()
{
网络适配器na=null;
foreach(NetworkInterface.GetAllNetworkInterfaces()中的NetworkInterface适配器)
{
IPInterfaceProperties properties=adapter.GetIPProperties();//获取网络配置属性
foreach(IPAddressInformation在properties.UnicastAddresses中单播)
{
if(!IPAddress.IsLoopback(uniCast.Address)&&uniCast.Address.AddressFamily!=AddressFamily.InterNetworkV6)//忽略回送地址和IPv6 internet协议族
{
na=新网络适配器{
Name=adapter.Name;
Id=adapter.Id;
Description=适配器。Description;
IpAddress=uniCast.Address.ToString();
GatewayIpAddress=adapter.NetworkInterfaceType.ToString();
Speed=适配器.Speed.ToString(“#,#0”);
NetworkInterfaceType=adapter.GetPhysicalAddress().ToString();
};
foreach(适配器中的GatewayIPAddress信息gatewayAddress.GetIPProperties().GatewayAddresses)
{
na.GatewayIpAddress=gatewayAddress.Address.ToString();
}
}
}
收益率;
}
}
}
您可以使用linq:
return from adapter in NetworkInterface.GetAllNetworkInterfaces()
from uniCast in adapter.GetIPProperties().UnicastAddresses
where !IPAddress.IsLoopback(uniCast.Address) && uniCast.Address.AddressFamily != AddressFamily.InterNetworkV6
let lastGatewayAddress = adapter.GetIPProperties().GatewayAddresses.LastOrDefault()
select new NetworkAdapter
{
_name = adapter.Name,
_id = adapter.Id,
_description = adapter.Description,
_ipAddress = uniCast.Address.ToString(),
_networkInterfaceType = adapter.NetworkInterfaceType.ToString(),
_speed = adapter.Speed.ToString("#,##0"),
_macAddress = adapter.GetPhysicalAddress().ToString(),
_gatewayIpAddress = lastGatewayAddress == null ? null : lastGatewayAddress.Address.ToString()
};
现在还不清楚你想用网关地址做什么。现在看来你想要最后一个