2012-01-09 107 views
1

我要檢索的MAC與IP地址地址...Retrive MAC與IP地址地址使用LINQ

var nics = NetworkInterface.GetAllNetworkInterfaces() 
    .Where(ipProp => ipProp.GetIPProperties().UnicastAddresses 
        .Where(ip => ip.Address.ToString().Equals("192.168.1.111")) 
    ); 

我得到一個錯誤:

"Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'bool'"

我該如何解決這個問題?

+3

在那裏有問題嗎? :)編輯:現在有。 – 2012-01-09 13:09:56

回答

3
NetworkInterface networkInterface = NetworkInterface.GetAllNetworkInterfaces().Where(ipProp => ipProp.GetIPProperties().UnicastAddresses.FirstOrDefault(ip => ip.Address.ToString().Equals("YOUR_IP")) != null).FirstOrDefault(); 
if (networkInterface != null) 
{ 
    Console.WriteLine(networkInterface.GetPhysicalAddress()); 
} 
+0

哦謝謝!如果想要這行返回MAC地址不是NetworkInterface,它可以嗎? – 2012-01-09 13:17:35

+0

查看更新的答案。 – ken2k 2012-01-09 13:20:04