public void SettingRestore(string destination, string hostfile_location, bool restroreDNS) { FileAttributes hostattributes = File.GetAttributes(hostfile_location); if (hostattributes.ToString().Contains(FileAttributes.Hidden.ToString()) || hostattributes.ToString().Contains(FileAttributes.System.ToString())) { File.SetAttributes(hostfile_location, FileAttributes.Normal); } try { System.IO.File.Copy(destination + "host", hostfile_location, true); // Copy the backup host file to that of the present host file . } catch { Thread.Sleep(1000); System.IO.File.Copy(destination + "host", hostfile_location, true); } if (restroreDNS) // If dns was set to that of OpenDNS then change the dns to that of old one. { string DNS_present; List <string> nullenumerable = new List <string>(); DNS_present = File.ReadAllText(destination + "DNSBackUp.bin"); string[] oldDNS = DNS_present.Split(new String[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); using (var NWconManager = new ManagementClass("Win32_NetworkAdapterConfiguration")) { using (var NWconfig = NWconManager.GetInstances()) { foreach (var managementObject in NWconfig.Cast <ManagementObject>().Where(managmentObject => (bool)managmentObject["IPEnabled"])) { ManagementBaseObject managementBase = managementObject.GetMethodParameters("SetDNSServerSearchOrder"); if (managementBase != null) { managementBase["DNSServerSearchOrder"] = oldDNS; managementObject.InvokeMethod("SetDNSServerSearchOrder", managementBase, null); } } } } nullenumerable.Add(""); //File.WriteAllLines(destination + "DNSBackUp.bin", nullenumerable); File.Create(destination + "DNSBackUp.bin"); } hostattributes = File.GetAttributes(hostfile_location); if (hostattributes.ToString().Contains(FileAttributes.Hidden.ToString()) || hostattributes.ToString().Contains(FileAttributes.System.ToString())) { File.SetAttributes(hostfile_location, FileAttributes.Normal); } if (PasswordRequire.firstRun) { CustomerSurvey surveyObj = new CustomerSurvey(); surveyObj.Show(); } }
void RestoreSetting_Loaded(object sender, RoutedEventArgs e) // Here we are checking if the DNS needs to be restored or not . { ManagementScope oMs = new ManagementScope("\\\\localhost\\root\\cimv2"); string strQuery = "select DNSServerSearchOrder from Win32_NetworkAdapterConfiguration where IPEnabled='true'"; ObjectQuery oQ = new ObjectQuery(strQuery); ManagementObjectSearcher oS = new ManagementObjectSearcher(oMs, oQ); ManagementObjectCollection oRc = oS.Get(); IsolatedStorageFile dnsFile = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly, null, null); foreach (ManagementObject oR in oRc) { if (!(oR.Properties["DNSServerSearchOrder"].Value == null)) { foreach (string str in (Array)(oR.Properties["DNSServerSearchOrder"].Value)) { if (str == "208.67.222.222" || str == "208.67.220.220") // in Case if the user is using the OpenDns already { currentOpenDNS = true; } } } } if (currentOpenDNS) { string DNS_present; DNS_present = File.ReadAllText(PasswordRequire.filePath + "DNSBackUp.bin"); string[] oldDNS = DNS_present.Split(new String[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); foreach (string dns in oldDNS) { if (dns == "208.67.222.222" || dns == "208.67.220.220") // If this is true , then the user was already using openDNS { RestoreDNS.Visibility = System.Windows.Visibility.Hidden; break; } } } }