Get local DNS server addresses and domain name.

Here is a way to get the host, domain name, and DNS servers of local machine.

using System;
using System.Net;
using System.Collections;
using System.Runtime.InteropServices;
using MVPTools;

namespace MVPTools.Net
{
 /// <summary>
 /// Summary description for IPConfig.
 /// </summary>
 public class IPConfig
 {
  private string hostName;
  private string domainName;
  private IPAddress[] dnsServers;

  public IPConfig()
  {
   GetParms();
  }

  private void GetParms()
  {
   uint uintBufferSize = 0;
   ArrayList dnsIPList = new ArrayList();
   IPAddress dnsIP;
   Native.IP_ADDR_STRING DNSIP;

   //run the method once to find the size of the buffer required
   if( Native.GetNetworkParams(IntPtr.Zero , ref uintBufferSize) != 111 )
    throw new ApplicationException("Error calling GetNetworkParams().");
 
   //declare a space in unmanaged memory to hold the data
   IntPtr pBuffer = Marshal.AllocHGlobal((int)uintBufferSize);

   //run the function
   if( Native.GetNetworkParams( pBuffer, ref uintBufferSize ) !=0 )
    throw new ApplicationException("Error getting adapter info.");

   Native.FIXED_INFO FInfo = (Native.FIXED_INFO)Marshal.PtrToStructure(pBuffer, typeof(Native.FIXED_INFO));
   this.hostName = FInfo.HostName;
   this.domainName = FInfo.DomainName;

   //Get DNS Server IPs:
   DNSIP = FInfo.DnsServerList;
   
   dnsIP = GetIP(DNSIP.IpAddress.AddrString);
   if ( dnsIP != null )
    dnsIPList.Add(dnsIP);
   
   while( DNSIP.Next != IntPtr.Zero)
   {
    DNSIP = (Native.IP_ADDR_STRING)Marshal.PtrToStructure(DNSIP.Next, typeof(Native.IP_ADDR_STRING));
    dnsIP = GetIP(DNSIP.IpAddress.AddrString);
    if ( dnsIP != null )
     dnsIPList.Add(dnsIP);
    else
     break;
   }

   this.dnsServers = (IPAddress[])dnsIPList.ToArray(typeof(IPAddress));
   Marshal.FreeHGlobal(pBuffer);
  }

  private IPAddress GetIP(string ipString)
  {
   if ( ipString == null || ipString == "" )
    return null;

   try
   {
    return IPAddress.Parse(ipString);
   }
   catch
   {
    return null;
   }
  }

  public string HostName
  {
   get { return this.hostName; }
  }

  public string DomainName
  {
   get { return this.domainName; }
  }

  public IPAddress[] DnsServers
  {
   get { return this.dnsServers; }
  }
 }
}

–William

Advertisements
This entry was posted in C#. Bookmark the permalink.

6 Responses to Get local DNS server addresses and domain name.

  1. Unknown says:

    http://www.batteryfast.co.uk/laptop-ac-adapter/hp/nc8230.htm Hp nc8230 Adapter http://www.batteryfast.co.uk/laptop-ac-adapter/hp/nc8000.htm Hp nc8000 Adapter http://www.batteryfast.co.uk/laptop-ac-adapter/hp/nc8430.htm Hp nc8430 Adapter http://www.batteryfast.co.uk/laptop-ac-adapter/hp/nc6100.htm Hp nc6100 Adapter http://www.batteryfast.co.uk/laptop-ac-adapter/hp/nc6120.htm Hp nc6120 Adapter http://www.batteryfast.co.uk/laptop-ac-adapter/hp/zt1000.htm Hp zt1000 Adapter http://www.batteryfast.co.uk/laptop-ac-adapter/hp/dv9600.htm Hp dv9600 Adapter http://www.batteryfast.co.uk/laptop-ac-adapter/hp/dv9700.htm Hp dv9700 Adapter http://www.batteryfast.co.uk/laptop-ac-adapter/hp/zv5000.htm Hp zv5000 Adapter http://www.batteryfast.co.uk/laptop-ac-adapter/hp/nx9600.htm Hp nx9600 Adapter http://www.batteryfast.co.uk/laptop-ac-adapter/ibm/t40.htm Ibm t40 Adapter http://www.batteryfast.co.uk/laptop-ac-adapter/ibm/x30.htm Ibm x30 Adapter http://www.batteryfast.co.uk/laptop-ac-adapter/ibm/x60.htm Ibm x60 Adapter http://www.batteryfast.co.uk/laptop-ac-adapter/liteon/m320.htm Liteon m320 Adapter http://www.batteryfast.co.uk/laptop-ac-adapter/liteon/m680.htm Liteon m680 Adapter http://www.batteryfast.co.uk/laptop-ac-adapter/liteon/m360.htm Liteon m360 Adapter http://www.batteryfast.co.uk/laptop-ac-adapter/liteon/m460.htm Liteon m460 Adapter http://www.batteryfast.co.uk/laptop-ac-adapter/liteon/m500.htm Liteon m500 Adapter http://www.batteryfast.co.uk/laptop-ac-adapter/liteon/aspire-1200.htm Liteon aspire 1200 Adapter http://www.batteryfast.co.uk/laptop-ac-adapter/liteon/presario-1200.htm Liteon presario 1200 Adapter http://www.batteryfast.co.uk/laptop-ac-adapter/liteon/presario-1600.htm Liteon presario 1600 Adapter http://www.batteryfast.co.uk/laptop-ac-adapter/liteon/presario-1800.htm Liteon presario 1800 Adapter http://www.batteryfast.co.uk/laptop-ac-adapter/liteon/2200.htm Liteon 2200 Adapter http://www.batteryfast.co.uk/laptop-ac-adapter/liteon/r3000.htm Liteon r3000 Adapter http://www.batteryfast.co.uk/laptop-ac-adapter/liteon/m3.htm Liteon m3 Adapter http://www.batteryfast.co.uk/laptop-ac-adapter/liteon/1500.htm Liteon 1500 Adapter http://www.batteryfast.co.uk/laptop-ac-adapter/liteon/1520.htm Liteon 1520 Adapter http://www.batteryfast.co.uk/laptop-ac-adapter/liteon/1600.htm Liteon 1600 Adapter http://www.batteryfast.co.uk/laptop-ac-adapter/liteon/1680.htm Liteon 1680 Adapter http://www.batteryfast.co.uk/laptop-ac-adapter/liteon/nx9500.htm Liteon nx9500 Adapter http://www.batteryfast.co.uk/laptop-ac-adapter/liteon/r3000-series.htm Liteon r3000 series Adapter http://www.batteryfast.co.uk/laptop-ac-adapter/liteon/travelmate-4200.htm Liteon travelmate 4200 Adapter http://www.batteryfast.co.uk/laptop-ac-adapter/liteon/5620.htm Liteon 5620 Adapter

  2. Unknown says:

    http://www.adapterlist.com/asus/m68.htm asus m68 battery http://www.adapterlist.com/asus/m68n.htm asus m68n battery http://www.adapterlist.com/asus/m6000.htm asus m6000 battery http://www.adapterlist.com/asus/a42-m6-silver.htm asus a42-m6 silver battery http://www.adapterlist.com/asus/m6000-silver.htm asus m6000 silver battery http://www.adapterlist.com/asus/m6-silver.htm asus m6 silver battery http://www.adapterlist.com/clevo/m540.htm clevo m540 battery http://www.adapterlist.com/clevo/mobinote-m-54g.htm clevo mobinote m 54g battery http://www.adapterlist.com/clevo/mobinote-m54v.htm clevo mobinote m54v battery http://www.adapterlist.com/clevo/mobinote-m-55g.htm clevo mobinote m 55g battery http://www.adapterlist.com/clevo/mobinote-m55v.htm clevo mobinote m55v battery http://www.adapterlist.com/clevo/mobinote-m-540g.htm clevo mobinote m 540g battery http://www.adapterlist.com/clevo/mobinote-m540v.htm clevo mobinote m540v battery http://www.adapterlist.com/clevo/mobinote-m-541g.htm clevo mobinote m 541g battery http://www.adapterlist.com/clevo/mobinote-m541v.htm clevo mobinote m541v battery http://www.adapterlist.com/clevo/mobinote-m-545g.htm clevo mobinote m 545g battery http://www.adapterlist.com/clevo/mobinote-m545v.htm clevo mobinote m545v battery http://www.adapterlist.com/clevo/mobinote-m-550g.htm clevo mobinote m 550g battery http://www.adapterlist.com/clevo/mobinote-m550v.htm clevo mobinote m550v battery http://www.adapterlist.com/clevo/mobinote-m-551g.htm clevo mobinote m 551g battery http://www.adapterlist.com/clevo/mobinote-m551v.htm clevo mobinote m551v battery http://www.adapterlist.com/clevo/mobinote-m-555g.htm clevo mobinote m 555g battery http://www.adapterlist.com/clevo/mobinote-m555v-serie.htm clevo mobinote m555v serie battery http://www.adapterlist.com/compaq/presario-1200.htm compaq presario 1200 battery http://www.adapterlist.com/compaq/presario-1600.htm compaq presario 1600 battery http://www.adapterlist.com/compaq/presario-1800.htm compaq presario 1800 battery http://www.adapterlist.com/compaq/nw8000.htm compaq nw8000 battery http://www.adapterlist.com/compaq/nc8000.htm compaq nc8000 battery http://www.adapterlist.com/compaq/v1000.htm compaq v1000 battery http://www.adapterlist.com/compaq/nc6000.htm compaq nc6000 battery http://www.adapterlist.com/compaq/nx5000.htm compaq nx5000 battery http://www.adapterlist.com/compaq/n600.htm compaq n600 battery http://www.adapterlist.com/compaq/n600c.htm compaq n600c battery http://www.adapterlist.com/compaq/n610c.htm compaq n610c battery http://www.adapterlist.com/compaq/n610v.htm compaq n610v battery http://www.adapterlist.com/compaq/n620c.htm compaq n620c battery http://www.adapterlist.com/compaq/nx9000.htm compaq nx9000 battery http://www.adapterlist.com/compaq/nx9005.htm compaq nx9005 battery http://www.adapterlist.com/compaq/nx9010.htm compaq nx9010 battery http://www.adapterlist.com/compaq/nx9020.htm compaq nx9020 battery http://www.adapterlist.com/compaq/hstnnib12.htm compaq hstnnib12 battery http://www.adapterlist.com/compaq/nc4200.htm compaq nc4200 battery http://www.adapterlist.com/compaq/nc4400.htm compaq nc4400 battery http://www.adapterlist.com/compaq/tc4200.htm compaq tc4200 battery http://www.adapterlist.com/compaq/tc4400.htm compaq tc4400 battery http://www.adapterlist.com/compaq/hstnnib12.htm compaq hstnnib12 battery

  3. Unknown says:

    Serena Williams and the season of her contentwow gold,world of warcraft gold,buy wow gold,wow power leveling,cheap wow gold,world of warcraft power
    leveling,world of warcraft gold,buy wow
    gold,ffxi gil,buy
    wow gold,wow power
    leveling,ffxi gil,world
    of warcraft power leveling,sell wow gold,wow power
    level,wow gold for
    sale,power leveling,wow
    power level,wow power level,power leveling,wow gold for
    sale,buy cheap wow gold.Indian brides confront gold bears, again

  4. Unknown says:

    Serena Williams and the season of her contentwow gold,world of warcraft gold,buy wow gold,wow power leveling,cheap wow gold,world of warcraft power
    leveling,world of warcraft gold,buy wow
    gold,ffxi gil,buy
    wow gold,wow power
    leveling,ffxi gil,world
    of warcraft power leveling,sell wow gold,wow power
    level,wow gold for
    sale,power leveling,wow
    power level,wow power level,power leveling,wow gold for
    sale,buy cheap wow gold.Indian brides confront gold bears, again

  5. Unknown says:

    Battery fit ASUS 90-NDT1B1000Z 90-NDT1B2000Z A32-M9 NEW laptop battery
    Battery For ASUS M3 M3N M3NP M3000 M3000N M3000NP 4S2P black laptop battery
    Battery For ASUS M3 M3N M3NP M3000 M3000N M3000NP 4S2P Silver laptop battery

    NEW ASUS M6 M6N M67N M68N BATTERY 90-N951B1000 A42-M6 Black laptop battery
    NEW ASUS M6 M6N M67N M68N BATTERY 90-N951B1000 A42-M6 Silver laptop battery
    Battery For ASUS A42-W1 W1 W1G W1N W1000 W1000G black laptop battery
    Battery For ASUS A42-W1 W1000Gc W1000N W1000Na W1000V silver laptop battery
    A42-L4 Asus 4 L4E L4H L4L L4R L41 L42 L44 L45 laptop battery
    Battery For ASUS A42-L5 L5C L5D L5G L5000 L5500 A42l5 laptop battery

    New Battery fit Asus A42-V6 VX1 V6 V6V V6000 V6000V laptop battery
    New Battery For CLEVO 2200 2700T M220S BAT-2296 BAT2794 laptop battery
    Battery for NC4200 NC4400 TC4200 383510-001 laptop battery
    New Battery For Compaq Presario 700 1400 Evo N105 N115 laptop battery
    New Battery for COMPAQ EVO N400 N400c N410c 213282-001 laptop battery

  6. Evan says:

    Hey William,
     
    Any chance of getting your Native class code?  It looks like that is your PInvoke wrapper, correct?  I\’m a little unfamiliar with unmanaged so…
     
    Thanks,
     
    Evan

Comments are closed.