I wrote this some time ago:
/// <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.GetNetwo rkParams(IntPtr .Zero , ref uintBufferSize) != 111 )
throw new ApplicationExce ption("Error calling GetNetworkParam s().");
//declare a space in unmanaged memory to hold the data
IntPtr pBuffer = Marshal.AllocHG lobal((int)uint BufferSize);
//run the function
if( Native.GetNetwo rkParams( pBuffer, ref uintBufferSize ) !=0 )
throw new ApplicationExce ption("Error getting adapter info.");
Native.FIXED_IN FO FInfo =
(Native.FIXED_I NFO)Marshal.Ptr ToStructure(pBu ffer,
typeof(Native.F IXED_INFO));
this.hostName = FInfo.HostName;
this.domainName = FInfo.DomainNam e;
//Get DNS Server IPs:
DNSIP = FInfo.DnsServer List;
dnsIP = GetIP(DNSIP.IpA ddress.AddrStri ng);
if ( dnsIP != null )
dnsIPList.Add(d nsIP);
while( DNSIP.Next != IntPtr.Zero)
{
DNSIP = (Native.IP_ADDR _STRING)Marshal .PtrToStructure (DNSIP.Next,
typeof(Native.I P_ADDR_STRING)) ;
dnsIP = GetIP(DNSIP.IpA ddress.AddrStri ng);
if ( dnsIP != null )
dnsIPList.Add(d nsIP);
else
break;
}
this.dnsServers = (IPAddress[])dnsIPList.ToAr ray(typeof(IPAd dress));
Marshal.FreeHGl obal(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 Stacey, MVP
http://mvp.support.microsoft.com
"Peter Gloor" <p_*****@hotmai l.com> wrote in message
news:eJ******** ******@TK2MSFTN GP09.phx.gbl...
I'm writing an application that requires some dns queries (MX etc) not
provided by System.Net.Dns. So far I managed to write my own dns class
with methods to query a given dns server.
There is only one thing left I could not figure out so far. How can I get
the list of dns servers used by my computer? The code must work under any
Windows version with .NET framework support.
Any ideas are welcome.
Peter