問題
使用 System.Management取得IP資訊
解決方法
using System.Management; public void GetIPNotUsingSystemNety() { ManagementObjectSearcher query = new ManagementObjectSearcher(@"SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'TRUE'"); ManagementObjectCollection queryCollection = query.Get(); string ans = "\r\n"; foreach (ManagementObject mo in queryCollection) { string[] addresses = (string[])mo["IPAddress"]; string[] subnets = (string[])mo["IPSubnet"]; string[] defaultgateways = (string[])mo["DefaultIPGateway"]; ans += "Network Card: " + mo["Description"] + "\r\n"; for (int i = 0; i<=addresses.GetLength(0); i++) { try{ ans += " IP Address: " + addresses[i].ToString() + "\r\n"; } catch (Exception ex){ ans += " IP Address: " + "\r\n"; } try{ ans += " Subnet Mask: " + subnets[i].ToString() + "\r\n"; } catch (Exception ex){ ans += " Subnet Mask: " + "\r\n"; } try{ ans += " Gateway: " + defaultgateways[i].ToString() + "\r\n"; } catch (Exception ex){ ans += " Gateway: " + "\r\n"; } } ans += " MAC Address: " + mo["MACAddress"] + "\r\n"; ans += " DNS Address: " + mo["DNSHostName"] + "\r\n"; /* ans += " DNS Address: " + Dns.GetHostAddresses(mo["DNSHostName"].ToString())[0].ToString() + "\r\n";ans += " DHCP DHCPEnabled : " + mo["DHCPEnabled"] + "\r\n"; */ ans += " DHCP Server Address: " + mo["DHCPServer"] + "\r\n"; } }
沒有留言:
張貼留言