Получить перечень доступных сетевых устройств в C#, используя и не используя SharpPcap
Определим структуру для хранения необходимой нам информации:
public struct device
{
public string ID; //ID
public string Name; //Наименование сетевой
public string Description; //описание
public IPAddress IP;
public IPAddress[] Gateway; //Шлюз
public IPAddress Mask; //Маска
public IPAddress[] DHCP; //Маска
public IPAddress[] DNS; //Маска
public string MAC; //MAC адрес
public Object obj;
}
Теперь получаем информацию об адаптерах без использования SharpPcap и заполняем наш массив адаптеров согласно структуры device:
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
device[] devices = new device[nics.Length];
for (int x = 0; x <= nics.Length - 1; x++)
{
devices[x] = new device();
if (nics[x].GetIPProperties().UnicastAddresses.Count != 0)
{
devices[x].IP = nics[x].GetIPProperties().UnicastAddresses[0].Address; ;
if (nics[x].GetIPProperties().UnicastAddresses[0].IPv4Mask != null)
devices[x].Mask = nics[x].GetIPProperties().UnicastAddresses[0].IPv4Mask;
}
devices[x].ID = nics[x].Id;
devices[x].Description = nics[x].Description;
devices[x].Name = nics[x].Name;
devices[x].MAC = nics[x].GetPhysicalAddress().ToString();
//Получить список DHCP адресов
if (nics[x].GetIPProperties().DhcpServerAddresses.Count != 0)
{
devices[x].DHCP = new System.Net.IPAddress[nics[x].GetIPProperties().DhcpServerAddresses.Count];
for (int y = 0; y <= nics[x].GetIPProperties().DhcpServerAddresses.Count - 1; y++)
{
devices[x].DHCP[y] = nics[x].GetIPProperties().DhcpServerAddresses[y];
}
}
//Получить список DNS адресов
if (nics[x].GetIPProperties().DnsAddresses.Count != 0)
{
devices[x].DNS = new System.Net.IPAddress[nics[x].GetIPProperties().DnsAddresses.Count];
for (int y = 0; y <= nics[x].GetIPProperties().DnsAddresses.Count - 1; y++)
{
devices[x].DNS[y] = nics[x].GetIPProperties().DnsAddresses[y];
}
}
//Получить список шлюзов
if (nics[x].GetIPProperties().GatewayAddresses.Count != 0)
{
devices[x].Gateway = new System.Net.IPAddress[nics[x].GetIPProperties().GatewayAddresses.Count];
for (int y = 0; y <= nics[x].GetIPProperties().GatewayAddresses.Count - 1; y++)
{
devices[x].Gateway[y] = nics[x].GetIPProperties().GatewayAddresses[y].Address;
}
}
} А это пример получения информации о сетевых адаптерах используя SharpPcap версии 4.2 в формат описанной нами структуры device
devices_list = CaptureDeviceList.Instance;
device[] devices = new device[devices_list.Count];
for (int x = 0; x <= devices_list.Count - 1; x++)
{
devices_list[x].Open();
devices[x].Name = ((PcapDevice)devices_list[x]).Interface.FriendlyName;
devices[x].ID = ((PcapDevice)devices_list[x]).Interface.Name;
devices[x].Description = ((LibPcapLiveDevice)devices_list[x]).Interface.Description;
devices[x].MAC = devices_list[x].MacAddress.ToString();
devices[x].IP = ((LibPcapLiveDevice)devices_list[x]).Addresses[0].Addr.ipAddress;
devices[x].Mask = ((LibPcapLiveDevice)devices_list[x]).Addresses[0].Netmask.ipAddress;
if (((PcapDevice)devices_list[x]).Interface.GatewayAddress != null)
{
devices[x].Gateway = new System.Net.IPAddress[1];
devices[x].Gateway[0] = ((PcapDevice)devices_list[x]).Interface.GatewayAddress;
}
devices_list[x].Close();
}