In my class, I have a method that loops through all network interfaces and adds values to three different lists. I then take those three individual lists and I add them to one combined list so that I can work on them in a different solution.
This is where I get lost. I want to use the get accessor to retrieve the lists from the GetAllNetworkInfo
method per network interface, meaning for each network interface I want to get the three lists. I would think that this should be a simple answer but I have not used get/set before and my mind is drawing a blank.
Can this be done? If so, how?
Here is what I have so far:
public class NetworkInformation
{
private static List<string> _listOfIPs = new List<string>();
private static List<string> _listOfSubnets = new List<string>();
private static List<string> _listOfGateways = new List<string>();
private static List<List<string>> _myList = new List<List<string>>();
public static object GetAllNetworkInfo()
{
NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface networkInterface in networkInterfaces)
{
IPInterfaceProperties adapterProperties = networkInterface.GetIPProperties();
GatewayIPAddressInformationCollection addresses = adapterProperties.GatewayAddresses;
if (networkInterface.OperationalStatus == OperationalStatus.Up)
{
UnicastIPAddressInformationCollection unicastIPC = networkInterface.GetIPProperties().UnicastAddresses;
foreach (UnicastIPAddressInformation unicast in unicastIPC)
{
if (unicast.Address.AddressFamily == AddressFamily.InterNetwork)
{
_listOfIPs.Add(unicast.Address.ToString());
_listOfSubnets.Add(unicast.IPv4Mask.ToString());
}
if (addresses.Count > 0)
{
foreach (GatewayIPAddressInformation address in addresses)
{
_listOfGateways.Add(address.Address.ToString());
}
}
}
}
_dict.Add(iP, subnet);
_myList.Add(_listOfIPs);
_myList.Add(_listOfGateways);
_myList.Add(_listOfSubnets);
}
return _myList;
}
//this is my blind attempt to get the values. Not sure if this will even work
public static List<string> IPAddressList
{
get
{
return _listOfIPs;
}
}
public static List<string> SubnetList
{
get
{
return _listOfSubnets;
}
}
public static List<string> GatewayList
{
get
{
return _listOfGateways;
}
}
You're looking to do something like this:
public class NetworkInformation
{
private static Dictionary<string, List<string>> _listOfIPs = null;
private static Dictionary<string, List<string>> _listOfSubnets = null;
private static Dictionary<string, List<string>> _listOfGateways = null;
private static List<Dictionary<string, List<string>>> _myList = new List<Dictionary<string, List<string>>>();
public static object GetAllNetworkInfo()
{
if ( _listOfIPs == null || _listOfSubnets == null || _listofGateways == null ) {
_listOfIPs = new Dictionary<string, List<string>();
_listOfSubnets = new Dictionary<string, List<string>();
_listOfGateways = new Dictionary<string, List<string>();
} else {
_listOfIPs.Clear();
_listOfSubnets.Clear();
_listOfGateways.Clear();
}
_myList.Clear();
NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface networkInterface in networkInterfaces)
{
_listOfIPs.Add( networkInterface.Name, new List<string>);
_listOfSubnets.Add( networkInterface.Name, new List<string>);
_listOfGateways.Add( neworkdInterface.Name, new List<string>);
IPInterfaceProperties adapterProperties = networkInterface.GetIPProperties();
GatewayIPAddressInformationCollection addresses = adapterProperties.GatewayAddresses;
if (networkInterface.OperationalStatus == OperationalStatus.Up)
{
UnicastIPAddressInformationCollection unicastIPC = networkInterface.GetIPProperties().UnicastAddresses;
foreach (UnicastIPAddressInformation unicast in unicastIPC)
{
if (unicast.Address.AddressFamily == AddressFamily.InterNetwork)
{
_listOfIPs[ networkInterface.Name ].Add(unicast.Address.ToString());
_listOfSubnets[ networkInterface.Name].Add(unicast.IPv4Mask.ToString());
}
if (addresses.Count > 0)
{
foreach (GatewayIPAddressInformation address in addresses)
{
_listOfGateways[ networkInterface.Name ].Add( address.Address.ToString());
}
}
}
}
_dict.Add(iP, subnet);
_myList.Add(_listOfIPs);
_myList.Add(_listOfGateways);
_myList.Add(_listOfSubnets);
}
return _myList;
}
public static Dictionary<string,string> IPAddressList
{
get
{
if ( _listOfIPs == null || _listofSubnets == null || _listOfGateways == null )
GetAllNetworkInfo()
return _listOfIPs;
}
}
public static Dictionary<string,string> SubnetList
{
get
{
if ( _listOfIPs == null || _listOfSubnets == null || _listOfGateways == null )
GetAllNetworkInfo()
return _listOfSubnets;
}
}
public static Dictionary<string,string> GatewayList
{
get
{
if ( _listOfIPs == null || _listofSubnets == null || _listOfGateways == null )
GetAllNetworkInfo()
return _listOfGateways;
}
}
I switched the Lists
to Dictionaries
so the NetworkInterface
information wouldn't be lost. Frankly, I'm not sure that's what you're after, but it does preserve the information. Now you can query the Dictionary
by NetWorkInterface
name.
This time I changed the Dictionaries
into Dictionary<string, List<string>>
, which will hold more than one IP per Network Interface, for example.
We need a bit more clarification as to how you expect to use this code but there are a couple options available to you. You can change the data structures that you're storing your values in to something that can be sorted "per network interface". For example:
private static Dictionary<string, List<string>> _interfaceIpAddresses
And then use the name of the network interface as the key to your dictionary as you loop over all the values.
if (unicast.Address.AddressFamily == AddressFamily.InterNetwork)
{
_interfaceIpAddresses[networkInterface.Name].Add(unicast.Address.ToString());
}
You could then change your getter to return the dictionary and you would have a sorted data structure containing all the IPAddresses etc indexed by their relative network interface.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.