public void LoadIpConfigInformation() { _m_AdapterInformationList = null; _m_CurrentAdapterInformationList = null; CAdapterInformationList.count = 0; //清理链表 /* * while(_m_AdapterInformationList != null) * { * _m_CurrentAdapterInformationList = m_AdapterInformationList; * _m_AdapterInformationList = _m_AdapterInformationList.m_NextAdapter; * * }*/ //获取所有网络接口放在adapters中。 System.Net.NetworkInformation.NetworkInterface[] adapters = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces(); foreach (System.Net.NetworkInformation.NetworkInterface adapter in adapters) { //未启用的网络接口不要 if (adapter.OperationalStatus != System.Net.NetworkInformation.OperationalStatus.Up) { continue; } //不是以太网和无线网的网络接口不要 if (adapter.NetworkInterfaceType != System.Net.NetworkInformation.NetworkInterfaceType.Ethernet && adapter.NetworkInterfaceType != System.Net.NetworkInformation.NetworkInterfaceType.Wireless80211 && adapter.NetworkInterfaceType != System.Net.NetworkInformation.NetworkInterfaceType.Ppp) { continue; } //虚拟机的网络接口不要 if (adapter.Name.IndexOf("VMware") != -1 || adapter.Name.IndexOf("Virtual") != -1) { continue; } CAdapterInformationList currentAdapter = new CAdapterInformationList(); //获取适配器名称 currentAdapter.m_AdapterInformation.m_Name = adapter.Name; //获取物理地址字节 byte[] physicalAddress = adapter.GetPhysicalAddress().GetAddressBytes(); if (physicalAddress.Length == 0 || physicalAddress.Length != 6) { currentAdapter.m_AdapterInformation.m_Physical = ""; } else { try { currentAdapter.m_AdapterInformation.m_Physical = String.Format("{0:x2}:{1:x2}:{2:x2}:{3:x2}:{4:x2}:{5:x2}", physicalAddress[0], physicalAddress[1], physicalAddress[2], physicalAddress[3], physicalAddress[4], physicalAddress[5]).ToUpper(); } catch (System.Exception) { } } //获取IP地址和Mask地址 System.Net.NetworkInformation.IPInterfaceProperties ipif = adapter.GetIPProperties(); System.Net.NetworkInformation.UnicastIPAddressInformationCollection ipifCollection = ipif.UnicastAddresses; foreach (System.Net.NetworkInformation.UnicastIPAddressInformation ipInformation in ipifCollection) { if (ipInformation.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { currentAdapter.m_AdapterInformation.m_IP = ipInformation.Address.ToString(); currentAdapter.m_AdapterInformation.m_Mask = ipInformation.IPv4Mask.ToString(); } } //获取网关地址,网关一般只有一个 if (ipif.GatewayAddresses != null && ipif.GatewayAddresses.Count != 0) { currentAdapter.m_AdapterInformation.m_Gateway = ((System.Net.NetworkInformation.GatewayIPAddressInformation)(ipif.GatewayAddresses[0])).Address.ToString(); } /* * foreach (System.Net.NetworkInformation.GatewayIPAddressInformation gwInformation in ipif.GatewayAddresses) * { * _m_Gateway = gwInformation.Address.ToString(); * } */ //获取DNS地址,DNS地址一般是两个,有可能没有或一个,或超过两个的情况 //如果超过两个只取前两个 StringBuilder dnsStr = new StringBuilder(30); foreach (System.Net.IPAddress dnsInformation in ipif.DnsAddresses) { dnsStr.Append(dnsInformation.ToString()); dnsStr.Append(','); } dnsStr.Remove(dnsStr.Length - 1, 1); string[] dnsArr = dnsStr.ToString().Split(','); #region switch折叠 switch (dnsArr.Length) { case 0: { currentAdapter.m_AdapterInformation.m_Dns1 = ""; currentAdapter.m_AdapterInformation.m_Dns2 = ""; break; } case 1: { currentAdapter.m_AdapterInformation.m_Dns1 = dnsArr[0]; currentAdapter.m_AdapterInformation.m_Dns2 = ""; break; } case 2: { currentAdapter.m_AdapterInformation.m_Dns1 = dnsArr[0]; currentAdapter.m_AdapterInformation.m_Dns2 = dnsArr[1]; break; } default: { currentAdapter.m_AdapterInformation.m_Dns1 = dnsArr[0]; currentAdapter.m_AdapterInformation.m_Dns2 = dnsArr[1]; break; } } if (_m_AdapterInformationList == null) { _m_AdapterInformationList = currentAdapter; ++CAdapterInformationList.count; //_m_AdapterInformationList.CAdapterInformationList = null; _m_AdapterInformationList.m_NextAdapter = null; _m_CurrentAdapterInformationList = _m_AdapterInformationList; } else { _m_CurrentAdapterInformationList.m_NextAdapter = currentAdapter; ++CAdapterInformationList.count; _m_CurrentAdapterInformationList = currentAdapter; } #endregion } }
public static void GetRightIPAndPort() { //HDLUDP.ConstPort = 6000; if (CsConst.myintProxy == 0) { string subnetMask = "255.255.255.0"; #region Boolean blnGetMask = NetworkInterface.GetIsNetworkAvailable(); //获取所有网络接口放在adapters中。 if (blnGetMask != false) { NetworkInterface[] adapters = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces(); foreach (System.Net.NetworkInformation.NetworkInterface adapter in adapters) { //未启用的网络接口不要 if (adapter.OperationalStatus != System.Net.NetworkInformation.OperationalStatus.Up) { continue; } //不是以太网和无线网的网络接口不要 if (adapter.NetworkInterfaceType != System.Net.NetworkInformation.NetworkInterfaceType.Ethernet && adapter.NetworkInterfaceType != System.Net.NetworkInformation.NetworkInterfaceType.Wireless80211 && adapter.NetworkInterfaceType != System.Net.NetworkInformation.NetworkInterfaceType.Ppp) { continue; } //虚拟机的网络接口不要 if (adapter.Name.IndexOf("VMware") != -1 || adapter.Name.IndexOf("Virtual") != -1) { continue; } //获取IP地址和Mask地址 System.Net.NetworkInformation.IPInterfaceProperties ipif = adapter.GetIPProperties(); System.Net.NetworkInformation.UnicastIPAddressInformationCollection ipifCollection = ipif.UnicastAddresses; foreach (System.Net.NetworkInformation.UnicastIPAddressInformation ipInformation in ipifCollection) { if (ipInformation.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { if (ipInformation.Address.ToString() == CsConst.myLocalIP) { subnetMask = ipInformation.IPv4Mask.ToString();//子网掩码 blnGetMask = true; break; } } } if (blnGetMask == true) { break; } } } #endregion byte[] ip = IPAddress.Parse(CsConst.myLocalIP).GetAddressBytes(); byte[] sub = IPAddress.Parse(subnetMask).GetAddressBytes(); // 广播地址=子网按位求反 再 或IP地址 for (int i = 0; i < ip.Length; i++) { ip[i] = (byte)((~sub[i]) | ip[i]); } //CsConst.myDestIP = "255.255.255.255"; CsConst.myDestIP = new IPAddress(ip).ToString(); } }