// Gets the initial settings for the NICs so we can set them back
    // on shutdown
    public static void SaveCurNICSettings()
    {
        // Should be null if never run. Should only be run once.
        if (m_NICPersisteSettings == null)
        {
            m_NICPersisteSettings = new List <NICObject>();
            NetworkInterface[] NICs = NetworkInterface.GetAllNetworkInterfaces();

            foreach (NetworkInterface NIC in NICs)
            {
                NICObject CurNIC = new NICObject(NIC, IsObtainDNSAuto(NIC.Id));
                m_NICPersisteSettings.Add(CurNIC);
            }
        }
    }
예제 #2
0
    // Gets the initial settings for the NICs so we can set them back
    // on shutdown
    public static void SaveCurNICSettings()
    {
        // Should be null if never run. Should only be run once.
        if (m_NICPersisteSettings == null)
        {
            m_NICPersisteSettings = new List<NICObject>();
            NetworkInterface[] NICs = NetworkInterface.GetAllNetworkInterfaces();

            foreach (NetworkInterface NIC in NICs)
            {
                NICObject CurNIC = new NICObject(NIC, IsObtainDNSAuto(NIC.Id));
                m_NICPersisteSettings.Add(CurNIC);
            }
        }
    }
    static void SetDNSDefault(NetworkInterface NIC)
    {
        // Get the state this guy was originally in
        NICObject UseNIC = null;

        foreach (NICObject OrigNIC in m_NICPersisteSettings)
        {
            if (OrigNIC.m_NIC.Id == NIC.Id)
            {
                UseNIC = OrigNIC;
            }
        }

        // Gets the adapter config and search to find DNS settings portion
        try
        {
            ManagementClass            mc  = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();

            foreach (ManagementObject mo in moc)
            {
                // Only look for this NIC (by ID)
                string sCurID = (string)mo["SettingID"];
                if ((sCurID == UseNIC.m_NIC.Id))
                {
                    // Get the DNS search order property
                    ManagementBaseObject objdns = mo.GetMethodParameters("SetDNSServerSearchOrder");
                    if (objdns != null)
                    {
                        // If we found the NIC in our saved ones above (should have)
                        // then use its original settings
                        if (UseNIC != null)
                        {
                            // SET TO ORIGINAL SETTINGS

                            // The original settings could have been "obtain DNS server automatically"
                            if (UseNIC.m_bIsObtainAuto)
                            {
                                // SET TO "obtain DNS server automatically"

                                // Call this guy with no "in" params sets it to default
                                object oRet = mo.InvokeMethod("SetDNSServerSearchOrder", null);
                            }
                            else
                            {
                                // SET TO HARD ADDRESS

                                IPAddressCollection IPColl         = UseNIC.m_NIC.GetIPProperties().DnsAddresses;
                                string[]            sDNSSearchList = new string[IPColl.Count];
                                int i = 0;
                                foreach (IPAddress CurIP in IPColl)
                                {
                                    sDNSSearchList[i++] = CurIP.ToString();
                                }

                                objdns["DNSServerSearchOrder"] = sDNSSearchList;
                                mo.InvokeMethod("SetDNSServerSearchOrder", objdns, null);
                            }
                        }
                        else
                        {
                            // SET TO "obtain DNS server automatically"

                            // Call this guy with no "in" params sets it to default
                            object oRet = mo.InvokeMethod("SetDNSServerSearchOrder", null);
                        }
                    }
                }
            }
        }
        catch (ManagementException e)
        {
            string sStack = e.ToString();
        }
    }