HostsFile.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // **********************************************************************************
  2. // CassiniDev - http://cassinidev.codeplex.com
  3. //
  4. // Copyright (c) 2010 Sky Sanders. All rights reserved.
  5. //
  6. // This source code is subject to terms and conditions of the Microsoft Public
  7. // License (Ms-PL). A copy of the license can be found in the license.txt file
  8. // included in this distribution.
  9. //
  10. // You must not remove this notice, or any other, from this software.
  11. //
  12. // **********************************************************************************
  13. #region
  14. using System;
  15. using System.Diagnostics;
  16. using System.IO;
  17. using System.Reflection;
  18. using System.Text.RegularExpressions;
  19. #endregion
  20. namespace CassiniDev
  21. {
  22. public static class HostsFile
  23. {
  24. /// <summary>
  25. ///
  26. /// </summary>
  27. /// <param name="ipAddress"></param>
  28. /// <param name="hostname"></param>
  29. /// <returns></returns>
  30. public static int AddHostEntry(string ipAddress, string hostname)
  31. {
  32. try
  33. {
  34. SetHostsEntry(true, ipAddress, hostname);
  35. return 0;
  36. }
  37. // ReSharper disable EmptyGeneralCatchClause
  38. catch
  39. // ReSharper restore EmptyGeneralCatchClause
  40. {
  41. }
  42. string executablePath = Assembly.GetExecutingAssembly().Location;
  43. return StartElevated(executablePath, string.Format("Hostsfile /ah+ /h:{0} /i:{1}", hostname, ipAddress));
  44. }
  45. /// <summary>
  46. ///
  47. /// </summary>
  48. /// <param name="ipAddress"></param>
  49. /// <param name="hostname"></param>
  50. /// <returns></returns>
  51. public static int RemoveHostEntry(string ipAddress, string hostname)
  52. {
  53. try
  54. {
  55. SetHostsEntry(false, ipAddress, hostname);
  56. return 0;
  57. }
  58. // ReSharper disable EmptyGeneralCatchClause
  59. catch
  60. // ReSharper restore EmptyGeneralCatchClause
  61. {
  62. }
  63. string executablePath = Assembly.GetExecutingAssembly().Location;
  64. return StartElevated(executablePath, string.Format("Hostsfile /ah- /h:{0} /i:{1}", hostname, ipAddress));
  65. }
  66. private static void SetHostsEntry(bool addHost, string ipAddress, string hostname)
  67. {
  68. // limitation: while windows allows mulitple entries for a single host, we currently allow only one
  69. string windir = Environment.GetEnvironmentVariable("SystemRoot") ?? @"c:\windows";
  70. string hostsFilePath = Path.Combine(windir, @"system32\drivers\etc\hosts");
  71. string hostsFileContent = File.ReadAllText(hostsFilePath);
  72. hostsFileContent = Regex.Replace(hostsFileContent,
  73. string.Format(@"\r\n^\s*[\d\w\.:]+\s{0}\s#\sadded\sby\scassini$",
  74. hostname), "", RegexOptions.Multiline);
  75. if (addHost)
  76. {
  77. hostsFileContent += string.Format("\r\n{0} {1} # added by cassini", ipAddress, hostname);
  78. }
  79. File.WriteAllText(hostsFilePath, hostsFileContent);
  80. }
  81. private static int StartElevated(string filename, string args)
  82. {
  83. ProcessStartInfo startInfo = new ProcessStartInfo
  84. {
  85. UseShellExecute = true,
  86. WorkingDirectory = Environment.CurrentDirectory,
  87. FileName = filename,
  88. Arguments = args,
  89. Verb = "runas"
  90. };
  91. try
  92. {
  93. Process p = Process.Start(startInfo);
  94. if (p != null)
  95. {
  96. p.WaitForExit();
  97. return p.ExitCode;
  98. }
  99. return -2;
  100. }
  101. catch
  102. {
  103. return -2;
  104. }
  105. }
  106. }
  107. }