/// <summary> /// Enable hosts file. /// </summary> public static void EnableHostsFile() { using (FileEx.DisableAttributes(DefaultDisabledHostFilePath, FileAttributes.ReadOnly)) { File.Move(DefaultDisabledHostFilePath, DefaultHostFilePath); } }
/// <summary> /// The disable hosts file. /// </summary> public static void DisableHostsFile() { using (FileEx.DisableAttributes(DefaultHostFilePath, FileAttributes.ReadOnly)) { File.Move(DefaultHostFilePath, DefaultDisabledHostFilePath); NativeMethods.FlushDns(); } }
/// <summary> /// Deletes the specified archive. /// </summary> /// <param name="archive">The archive.</param> public void Delete(HostsArchive archive) { using (FileEx.DisableAttributes(archive.FilePath, FileAttributes.ReadOnly)) { File.Delete(archive.FilePath); } this.Remove(archive); }
/// <summary> /// Save the hosts file to the specified file. /// </summary> /// <param name="saveFilePath"> /// The save file path. /// </param> public void SaveAs(string saveFilePath) { FileInfo info = new FileInfo(saveFilePath); if (!Directory.Exists(info.DirectoryName)) { Directory.CreateDirectory(info.DirectoryName); } using (FileEx.DisableAttributes(saveFilePath, FileAttributes.ReadOnly)) { File.WriteAllLines( saveFilePath, this.Entries.Select(entry => entry.UnparsedText)); } }
/// <summary> /// Initializes a new instance of the <see cref="HostsFile"/> class. /// </summary> /// <param name="filePath"> /// The file path. /// </param> private HostsFile(string filePath) { this.filePath = filePath; if (!File.Exists(filePath)) { this.Entries = new HostsEntryList(); } else { using (FileEx.DisableAttributes(DefaultBackupHostFilePath, FileAttributes.ReadOnly)) { File.Copy(filePath, DefaultBackupHostFilePath, true); } this.Entries = new HostsEntryList(File.ReadAllLines(filePath), RemoveDefaultText); } this.Entries.ListChanged += this.OnHostsEntriesListChanged; }