HostsHelper.cs 6.4 KB

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