Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Change DNS with C# on Windows 10

Tags:

c#

dns

I'm trying to change the DNS on Windows 10 through VB.NET.

I have code that works on Windows 7, however it does not work on Windows 10.

Here is my code for Windows 7 that changes the DNS:

ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
    if ((bool)mo["IPEnabled"])
    {
        ManagementBaseObject objdns = mo.GetMethodParameters("SetDNSServerSearchOrder");
        if (objdns != null)
        {
            string[] s = { "192.168.XX.X", "XXX.XX.X.XX" };
            objdns["DNSServerSearchOrder"] = s;
            mo.InvokeMethod("SetDNSServerSearchOrder", objdns, null);

My question is, how do I get this to work on Windows 10 OS?

like image 379
J doe Avatar asked Oct 27 '16 18:10

J doe


People also ask

How do I change my DNS from 8.8 8.8 to 11?

To change the DNS address on Windows 11, open Settings > Network & internet and select Ethernet or Wi-Fi. Then open the “DNS server assignment” settings and change the DNS address. Also, on Command Prompt, you can use the netsh interface ip set dns name="Ethernet0" static 1.1. 1.1 command.

How do I edit my DNS?

To change your DNS server on a Windows 10 computer, go to Settings > Network & Internet > Change Adapter Settings. Then right-click a connection and select Properties > IPv4 > Properties. Finally,select Use the following DNS server address.


1 Answers

First you need to get the NetworkInterface you want to set/unset DNS

I've tested this code on the latest version of Windows 10 and it works like a charm!

Here is the code to find the active Ethernet or Wifi network (Not 100% accurate but useful in most cases)

public static NetworkInterface GetActiveEthernetOrWifiNetworkInterface()
{
    var Nic = NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault(
        a => a.OperationalStatus == OperationalStatus.Up &&
        (a.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || a.NetworkInterfaceType == NetworkInterfaceType.Ethernet) &&
        a.GetIPProperties().GatewayAddresses.Any(g => g.Address.AddressFamily.ToString() == "InterNetwork"));

    return Nic;
}

SetDNS

public static void SetDNS(string DnsString)
{
    string[] Dns = { DnsString };
    var CurrentInterface = GetActiveEthernetOrWifiNetworkInterface();
    if (CurrentInterface == null) return;

    ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
    ManagementObjectCollection objMOC = objMC.GetInstances();
    foreach (ManagementObject objMO in objMOC)
    {
        if ((bool)objMO["IPEnabled"])
        {
            if (objMO["Description"].ToString().Equals(CurrentInterface.Description))
            {
                ManagementBaseObject objdns = objMO.GetMethodParameters("SetDNSServerSearchOrder");
                if (objdns != null)
                {
                    objdns["DNSServerSearchOrder"] = Dns;
                    objMO.InvokeMethod("SetDNSServerSearchOrder", objdns, null);
                }
            }
        }
    }
}

UnsetDNS

public static void UnsetDNS()
{
    var CurrentInterface = GetActiveEthernetOrWifiNetworkInterface();
        if (CurrentInterface == null) return;

    ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
    ManagementObjectCollection objMOC = objMC.GetInstances();
    foreach (ManagementObject objMO in objMOC)
    {
        if ((bool)objMO["IPEnabled"])
        {
            if (objMO["Description"].ToString().Equals(CurrentInterface.Description))
            {
                ManagementBaseObject objdns = objMO.GetMethodParameters("SetDNSServerSearchOrder");
                if (objdns != null)
                {
                    objdns["DNSServerSearchOrder"] = null;
                    objMO.InvokeMethod("SetDNSServerSearchOrder", objdns, null);
                }
            }
        }
    }
}

Usage

SetDNS("127.0.0.1");
like image 74
Mohamad Rashidi Avatar answered Sep 29 '22 17:09

Mohamad Rashidi