WMI Query to Get IP Address

var query = new System.Management.SelectQuery(
    @"SELECT *
    FROM Win32_NetworkAdapterConfiguration 
    WHERE IPEnabled = TRUE 
    OR DHCPEnabled = TRUE");
var searcher = new System.Management.ManagementObjectSearcher(query);
var netCards = searcher.Get();
foreach(var netCard in netCards)
{
    var ips = netCard["IPAddress"] as string[];
    if (ips != null)
    {
        System.Console.WriteLine("{0}\t{1}"
            , netCard["Description"]
            , netCard["MACAddress"]);
        foreach(var ip in ips)
        {
            System.Console.WriteLine("\t{0}", ip);
        }
    }
}

No hay comentarios:

Publicar un comentario