标签:
/// <summary> /// DNS、网关、子网掩码、IP /// 涂聚文 2015 /// </summary> public class IPAddressString { /// <summary> /// IP地址 /// </summary> private string _localAddress; /// <summary> /// 子网掩码 /// </summary> private string _ipSubnet; /// <summary> /// 默认网关 /// </summary> private string _defaultIPGateway; /// <summary> /// DNS /// </summary> private string _firstDNS; /// <summary> /// 备用DNS /// </summary> private string _secondDNS; /// <summary> /// /// </summary> private string _hostName; /// <summary> /// /// </summary> private string _macname; /// <summary> /// IP地址 /// </summary> public string LocalAddress { get { return _localAddress; } set { _localAddress = value; } } /// <summary> /// 子网掩码 /// </summary> public string IpSubnet { get { return _ipSubnet; } set { _ipSubnet = value; } } /// <summary> /// 默认网关 /// </summary> public string DefaultIPGateway { get { return _defaultIPGateway; } set { _defaultIPGateway = value; } } /// <summary> /// DNS /// </summary> public string FirstDNS { get { return _firstDNS; } set { _firstDNS = value; } } /// <summary> /// 备用DNS /// </summary> public string SecondDNS { get { return _secondDNS; } set { _secondDNS = value; } } /// <summary> /// /// </summary> public string HostName { get { return _hostName; } set { _hostName = value; } } /// <summary> /// /// </summary> public string Macname { get { return _macname; } set { _macname = value; } } } /// <summary> ///获取 DNS、网关、子网掩码、IP ///GEOVIN DU /// </summary> /// <returns></returns> public IPAddressString getIp() { IPAddressString ip = new IPAddressString(); ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection nics = mc.GetInstances(); foreach (ManagementObject nic in nics) { if (Convert.ToBoolean(nic["ipEnabled"]) == true) { // Get IP,SubNetMask,Gateway ip.LocalAddress = (nic["IPAddress"] as string[])[0]; ip.IpSubnet = (nic["IPSubnet"] as string[])[0]; ip.DefaultIPGateway = (nic["DefaultIPGateway"] as string[])[0]; ip.Macname = nic["MacAddress"].ToString(); ip.HostName = Dns.GetHostName();//计算机名 } } // Get DNS NetworkInterface[] ifs = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface netif in ifs) { IPInterfaceProperties properties = netif.GetIPProperties(); IPAddressCollection dnses = properties.DnsAddresses; if (dnses.Count > 0) { int i = 0; foreach (IPAddress ipAddr in dnses) { // Set DNS To DNS TextBox if (i == 0) ip.FirstDNS = ipAddr.ToString(); else ip.SecondDNS = ipAddr.ToString(); i++; } break; } } return ip; }
测试
//1 //IPAddress ip = Dns.GetHostAddresses(Dns.GetHostName()).Where(address => address.AddressFamily == AddressFamily.InterNetwork).First(); //MessageBox.Show(ip.ToString()); //2 //IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName()); //foreach (IPAddress addr in localIPs) //{ // if (addr.AddressFamily == AddressFamily.InterNetwork) // { // MessageBox.Show(addr.ToString()); // } //} //3. //IPAddress ip = GetIPAddress("dusystem.com"); //MessageBox.Show(ip.ToString()); //4. //List<string> ips = GetIPAddress(Dns.GetHostName()); //foreach (string s in ips) //{ // MessageBox.Show(s.ToString()); //} //5 IPAddressString ip = getIp(); MessageBox.Show(ip.IpSubnet);
标签:
原文地址:http://www.cnblogs.com/geovindu/p/4598144.html