суббота, 6 ноября 2010 г.

Получение информации о сетевых интерфейсах в C#

    public Form1()
    {
      InitializeComponent();

      textBox1.Text = NetworkInterfacesInformation();
    }

    private string NetworkInterfacesInformation()
    {
      string NetIntInfo = "";

      IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
      NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();

      NetIntInfo = NetIntInfo + "Имя компьютера: " + Environment.MachineName + ", Домен: " + Environment.UserDomainName + ", Пользователь: " + SystemInformation.UserName + Environment.NewLine;

      NetIntInfo = NetIntInfo + "Информация по сетевым интерфейсам компьютера " + computerProperties.HostName + computerProperties.DomainName + Environment.NewLine;

      foreach (NetworkInterface adapter in nics)
      {

        IPInterfaceProperties properties = adapter.GetIPProperties();
        NetIntInfo = NetIntInfo + String.Empty.PadLeft(adapter.Description.Length, '=') + Environment.NewLine;
        NetIntInfo = NetIntInfo + adapter.Description + Environment.NewLine;

        NetIntInfo = NetIntInfo + " Тип интерфейса\t: " + adapter.NetworkInterfaceType + Environment.NewLine;

        foreach (UnicastIPAddressInformation uniIPInfo in properties.UnicastAddresses)
        {
          NetIntInfo = NetIntInfo + " Адрес\t\t: " + uniIPInfo.Address.ToString() + Environment.NewLine;
          NetIntInfo = NetIntInfo + "\t\t\tPreferred Lifetime: " + uniIPInfo.AddressPreferredLifetime + Environment.NewLine;
          NetIntInfo = NetIntInfo + "\t\t\tValid Lifetime: " + uniIPInfo.AddressValidLifetime + Environment.NewLine;
          NetIntInfo = NetIntInfo + "\t\t\tDHCP Lease Lifetime: " + uniIPInfo.DhcpLeaseLifetime + Environment.NewLine;
          NetIntInfo = NetIntInfo + "\t\t\tPrefix Origin: " + uniIPInfo.PrefixOrigin.ToString() + Environment.NewLine;
          NetIntInfo = NetIntInfo + "\t\t\tSuffix Origin: " + uniIPInfo.SuffixOrigin.ToString() + Environment.NewLine;
        }

        NetIntInfo = NetIntInfo + " MAC Адрес\t: " + adapter.GetPhysicalAddress().ToString() + Environment.NewLine;
        NetIntInfo = NetIntInfo + " Is receive only\t: " + adapter.IsReceiveOnly + Environment.NewLine;
        NetIntInfo = NetIntInfo + " Multicast\t: " + adapter.SupportsMulticast + Environment.NewLine;
      }

      return NetIntInfo;
    }


* This source code was highlighted with Source Code Highlighter.

В результате получаем вот такой вывод