Csharp: Obtain DNS, gateway, subnet mask, IP address, csharpdns

Source: Internet
Author: User

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);

  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.