不显示系统的IP地址

时间:2013-02-05 06:21:16

标签: c# .net wmi

以下代码成功运行但我无法获取系统的IP地址。

 ManagementObjectSearcher mos=new ManagementObjectSearcher("select IPAddress,IPEnabled from win32_NetworkAdapterConfiguration where IPEnabled=true");
 ManagementObjectCollection moc=mos.Get();
 foreach(ManagementObject mo in moc)
 {
    Console.WriteLine("Variable = {0},Value = {1}  ",
         mo["IPAddress"],mo["IPEnabled"]);
 }

上面的代码结果为

  " Variable = System.String[]  ,  Value = True"  

你能告诉我如何获取系统的IP地址吗?

1 个答案:

答案 0 :(得分:2)

如果您想使用原始代码,请执行以下操作

ManagementObjectSearcher mos=new ManagementObjectSearcher("select IPAddress,IPEnabled from win32_NetworkAdapterConfiguration where IPEnabled=true");
 ManagementObjectCollection moc=mos.Get();
 foreach(ManagementObject mo in moc)
 {
    Console.WriteLine("Variable = {0},Value = {1}  ",
         (string[])mo["IPAddress"],mo["IPEnabled"]);
 }

如果您调试答案,当您将鼠标放在上时(此处此行(string [])mo [“IPAddress”] ],mo [“IPEnabled”])您将看到IP地址和Mac地址