kAdapterConfiguration获取IPAddress

kAdapterConfiguration获取IPAddress

本文介绍了如何从System.Management Win32_NetworkAdapterConfiguration获取IPAddress的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试检索IP地址。

但它给出了这个例外

对象引用未设置为实例一个对象。






am trying to retrieve ip address.
but it's giving this exception
"Object reference not set to an instance of an object."


ManagementScope scope = new ManagementScope("\\\\.\\ROOT\\cimv2");
            ObjectQuery query1 = new ObjectQuery("SELECT * FROM Win32_NetworkAdapterConfiguration");
            ManagementObjectSearcher searcher1 = new ManagementObjectSearcher(scope, query1);
            ManagementObjectCollection queryCollection = searcher1.Get();

            foreach (ManagementObject m in queryCollection)
            {

                listBox2.Items.Add(m["DefaultIPGateway"].ToString());
            }





任何人都可以解决此查询。

谢谢

sam.198979



can anyone solve this query.
thanks
sam.198979

推荐答案


ManagementScope scope = new ManagementScope(@"\\.\ROOT\CIMV2");
ObjectQuery query1 = new ObjectQuery("SELECT * FROM Win32_NetworkAdapterConfiguration");
ManagementObjectSearcher searcher1 = new ManagementObjectSearcher(scope, query1);
ManagementObjectCollection queryCollection = searcher1.Get();

foreach (ManagementObject m in queryCollection)
{
    string[] addr = (string[])m.GetPropertyValue("IPAddress");
    if (addr != null)
    {
        Console.WriteLine(addr[0]);
    };
}


这篇关于如何从System.Management Win32_NetworkAdapterConfiguration获取IPAddress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 01:09