HostsHelper.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Windows.Forms;
  8. namespace Optimizer {
  9. internal static class HostsHelper {
  10. internal static string NewLine = Environment.NewLine;
  11. internal static readonly string HostsFile = CleanHelper.System32Folder + "\\drivers\\etc\\hosts";
  12. internal static void RestoreDefaultHosts() {
  13. try {
  14. if (File.Exists(HostsFile)) {
  15. File.Delete(HostsFile);
  16. }
  17. File.WriteAllBytes(HostsFile, Properties.Resources.hosts);
  18. }
  19. catch (Exception ex) {
  20. Logger.LogError("HostsHelper.RestoreDefaultHosts", ex.Message, ex.StackTrace);
  21. MessageBox.Show(OptionsHelper.TranslationList("dnsCacheM").ToString(), "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  22. }
  23. }
  24. internal static string[] ReadHosts() {
  25. StringBuilder sb = new StringBuilder();
  26. try {
  27. using (StreamReader sr = File.OpenText(HostsFile)) {
  28. sb.Append(sr.ReadToEnd());
  29. }
  30. }
  31. catch (Exception ex) {
  32. Logger.LogError("HostsHelper.ReadHosts", ex.Message, ex.StackTrace);
  33. }
  34. return sb.ToString().Split(Environment.NewLine.ToCharArray());
  35. //return File.ReadAllLines(HostsFile);
  36. }
  37. internal static string ReadHostsFast() {
  38. StringBuilder sb = new StringBuilder();
  39. try {
  40. using (StreamReader sr = File.OpenText(HostsFile)) {
  41. sb.Append(sr.ReadToEnd());
  42. }
  43. }
  44. catch (Exception ex) {
  45. Logger.LogError("HostsHelper.ReadHostsFast", ex.Message, ex.StackTrace);
  46. }
  47. return sb.ToString();
  48. }
  49. internal static void LocateHosts() {
  50. Utilities.FindFile(HostsFile);
  51. }
  52. internal static void SaveHosts(string[] lines) {
  53. for (int i = 0; i < lines.Length; i++) {
  54. if (!lines[i].StartsWith("#") && (!string.IsNullOrEmpty(lines[i]))) {
  55. lines[i] = SanitizeEntry(lines[i]);
  56. }
  57. }
  58. try {
  59. File.WriteAllText(HostsFile, string.Empty);
  60. File.WriteAllLines(HostsFile, lines);
  61. }
  62. catch (Exception ex) {
  63. Logger.LogError("HostsHelper.SaveHosts", ex.Message, ex.StackTrace);
  64. MessageBox.Show(OptionsHelper.TranslationList("dnsCacheM").ToString(), "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  65. }
  66. }
  67. internal static List<string> GetHostsEntries() {
  68. List<string> entries = new List<string>();
  69. //string[] lines = File.ReadAllLines(HostsFile);
  70. string[] lines = ReadHosts();
  71. foreach (string line in lines) {
  72. if (!line.StartsWith("#") && (!string.IsNullOrEmpty(line))) {
  73. entries.Add(line.Replace(" ", " : "));
  74. }
  75. }
  76. return entries;
  77. }
  78. internal static void AddEntry(string entry) {
  79. try {
  80. File.AppendAllText(HostsFile, NewLine + $"{entry}");
  81. }
  82. catch (Exception ex) {
  83. Logger.LogError("HostsHelper.AddEntry", ex.Message, ex.StackTrace);
  84. MessageBox.Show(OptionsHelper.TranslationList("dnsCacheM").ToString(), "DNS Cache is running", MessageBoxButtons.OK, MessageBoxIcon.Information);
  85. }
  86. }
  87. internal static void RemoveEntry(string entry) {
  88. try {
  89. File.WriteAllLines(HostsFile, File.ReadLines(HostsFile).Where(x => x != entry).ToList());
  90. }
  91. catch (Exception ex) {
  92. Logger.LogError("HostsHelper.RemoveEntry", ex.Message, ex.StackTrace);
  93. MessageBox.Show(OptionsHelper.TranslationList("dnsCacheM").ToString(), "DNS Cache is running", MessageBoxButtons.OK, MessageBoxIcon.Information);
  94. }
  95. }
  96. internal static void RemoveEntryFromTemplate(string domain) {
  97. try {
  98. File.WriteAllLines(HostsFile, File.ReadLines(HostsFile).Where(x => !x.Contains(domain)).ToList());
  99. }
  100. catch (Exception ex) {
  101. Logger.LogError("HostsHelper.RemoveEntryFromTemplate", ex.Message, ex.StackTrace);
  102. }
  103. }
  104. internal static void RemoveAllEntries(List<string> collection) {
  105. try {
  106. foreach (string text in collection) {
  107. File.WriteAllLines(HostsFile, File.ReadLines(HostsFile).Where(l => l != text).ToList());
  108. }
  109. }
  110. catch (Exception ex) {
  111. Logger.LogError("HostsHelper.RemoveAllEntries", ex.Message, ex.StackTrace);
  112. MessageBox.Show(OptionsHelper.TranslationList("dnsCacheM").ToString(), "DNS Cache is running", MessageBoxButtons.OK, MessageBoxIcon.Information);
  113. }
  114. }
  115. internal static string SanitizeEntry(string entry) {
  116. // remove multiple white spaces and keep only one
  117. return Regex.Replace(entry, @"\s{2,}", " ");
  118. }
  119. internal static bool GetReadOnly() {
  120. try {
  121. return new FileInfo(HostsFile).IsReadOnly;
  122. }
  123. catch (Exception ex) {
  124. Logger.LogError("HostsHelper.ReadOnly", ex.Message, ex.StackTrace);
  125. return false;
  126. }
  127. }
  128. internal static void ReadOnly(bool enable) {
  129. try {
  130. FileInfo fi = new FileInfo(HostsFile);
  131. fi.IsReadOnly = enable;
  132. }
  133. catch (Exception ex) {
  134. Logger.LogError("HostsHelper.ReadOnly", ex.Message, ex.StackTrace);
  135. }
  136. }
  137. }
  138. }