using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.NetworkInformation;

namespace TarjetasRed
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Necesita declarar using System.Net.NetworkInformation
            ListViewItem registro; //Conjunto de datos de columnas para la ListView
            string[] strColumnas;
            string strNombreEstacion = "";
            int nRegs;
            int n;

            IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
            strNombreEstacion = String.Format("{0}.{1}",
                               computerProperties.HostName,
                               computerProperties.DomainName);
            txtNombreEstacion.Text = strNombreEstacion;
            txtNumTarjetasRed.Text = String.Format("{0}", nics.Length);

            if (nics == null || nics.Length < 1)
            {
                return;
            }
            listView1.Items.Clear();
            listView1.FullRowSelect = true;
            listView1.View = View.Details;
            listView1.Columns.Add("ID tarjeta");
            listView1.Columns.Add("Tarjeta");
            listView1.Columns.Add("Tipo");
            listView1.Columns.Add("Dirección física");
            listView1.Columns.Add("Estado");
            listView1.Columns.Add("IP");
            nRegs = 0;
            foreach (NetworkInterface adapter in nics)
            {
                nRegs++;

                IPInterfaceProperties properties = adapter.GetIPProperties();

                strColumnas = new string[6];

                strColumnas[0] = String.Format("{0}", adapter.Id);
                strColumnas[1] = adapter.Description;
                strColumnas[2] = String.Format("{0}", adapter.NetworkInterfaceType);
                strColumnas[3] = adapter.GetPhysicalAddress().ToString();
                strColumnas[4] = String.Format("{0}", adapter.OperationalStatus);
                if (properties.IsDnsEnabled)
                {
                    if (properties.DnsAddresses.Count > 0)
                    {
                        byte[] DirIP = properties.DnsAddresses[0].GetAddressBytes();
                        strColumnas[5] = String.Format("{0}.{1}.{2}.{3}", DirIP[0], DirIP[1], DirIP[2], DirIP[3]);
                    }
                }
                if (properties.DhcpServerAddresses.Count > 0)
                {
                    byte[] DirIP = properties.DhcpServerAddresses[0].GetAddressBytes();
                    strColumnas[5] = String.Format("{0}.{1}.{2}.{3}", DirIP[0], DirIP[1], DirIP[2], DirIP[3]);
                }
                registro = new ListViewItem(strColumnas);

                listView1.Items.Insert(nRegs - 1, registro);
                
                string versions = "";
            }

            //Ajustar anchos de columna automáticamente:
            for (n = 0; n < listView1.Columns.Count; n++)
            {
                listView1.Columns[n].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
            }

            return;
        }
    }
}
