【问题标题】:doesn't displays the ip address of the system不显示系统的ip地址
【发布时间】:2013-02-05 06:21:15
【问题描述】:

以下代码运行成功,但我无法获取系统的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 地址吗...?

【问题讨论】:

    标签: c# .net wmi


    【解决方案1】:

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

    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 地址

    【讨论】:

    • 很高兴能快速帮助您
    猜你喜欢
    • 2014-06-16
    • 2014-03-07
    • 1970-01-01
    • 2020-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多