Link to home
Start Free TrialLog in
Avatar of Frutarom IT
Frutarom IT

asked on

Local DNS Servers with C#

I am trying to find my local DNS Servers in C# and apply them to a string that uses Environment.NewLine to use a new line for the start of a new Server.

 public static IPAddress GetDnsAddress()
        {
            NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();

            foreach (NetworkInterface networkInterface in networkInterfaces)
            {
                if (networkInterface.OperationalStatus == OperationalStatus.Up)
                {
                    IPInterfaceProperties ipProperties = networkInterface.GetIPProperties();
                    IPAddressCollection dnsAddresses = ipProperties.DnsAddresses;

                    foreach (IPAddress dnsAdress in dnsAddresses)
                    {
                        return dnsAdress;
                    }
                }
            }

            throw new InvalidOperationException("Unable to find DNS Address");
        }

Open in new window



I have this from StackOverflow. I just can't seem how to figure out a way to apply it to a string with a NewLine at each new Server.
SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Frutarom IT
Frutarom IT

ASKER

I am looking more to apply it to a string so that I can set it equal to a Label.

This will do the same, but only able to output to the console. This does not work for the same way, and my alterations just give me errors.
In the end I want something like

Label1.Text += DnsServer + Environment.NewLine;

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
.... Console.WriteLine(someString); is equivalent to Label1.Text += someString;