Csharp: Obtain DNS, gateway, subnet mask, IP address, csharpdns
/// <Summary> /// DNS, gateway, subnet mask, IP // tu juwen 2015 /// </summary> public class IPAddressString {// <summary> /// IP address // </summary> private string _ localAddress; /// <summary> /// subnet mask /// </summary> private string _ ipSubnet; /// <summary> /// Default Gateway /// </summary> private string _ defaultIPGateway; /// <summary> /// DNS /// </summary> private string _ firstDNS; /// <summary> /// backup DNS /// </summary> private string _ secondDNS; /// <summary> ///// </summary> private string _ hostName; /// <summary> ///// </summary> private string _ macname; /// <summary> // ip address // </summary> public string LocalAddress {get {return _ localAddress;} set {_ localAddress = value ;}} /// <summary> /// subnet mask /// </summary> public string IpSubnet {get {return _ ipSubnet;} set {_ ipSubnet = value ;}} /// <summary >/// Default Gateway /// </summary> public string DefaultIPGateway {get {return _ defaultIPGateway;} set {_ defaultIPGateway = value ;}} /// <summary> // DNS // </summary> public string FirstDNS {get {return _ firstDNS;} set {_ firstDNS = value ;}} /// <summary> /// backup 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> /// obtain DNS, gateway, subnet mask, IP // geovin du /// </summary> /// <returns> </returns> public IPAddressString getIp () {IPAddressString ip = new IPAddressString (); ManagementClass mc = new ManagementClass ("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection criteria = mc. getInstances (); foreach (ManagementObject nic in seconds) {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 (); // computer name} // 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 ;}
Test
//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);