|
- class ModifyHosts
- {
- /// <summary>
- /// 新增hosts记录
- /// </summary>
- /// <param name="ip"></param>
- /// <param name="domain"></param>
- public static void Add_Hosts(string domain, string ip)
- {
- string path = Environment.GetFolderPath(Environment.SpecialFolder.System) + "\\drivers\\etc\\hosts";
- string[] hosts = File.ReadAllLines(path);
- List<string> list = hosts.ToList();
- string temp = hosts.ToList().FirstOrDefault(x => x.Contains(domain));
- if (string.IsNullOrEmpty(temp))
- {
- list.Add($"{ip} {domain}");
- }
- File.WriteAllLines(path, list.ToArray());
- }
- /// <summary>
- /// 更新Hosts记录
- /// </summary>
- /// <param name="ip"></param>
- /// <param name="domain"></param>
- public static void Update_Hosts(string domain, string ip)
- {
- string path = Environment.GetFolderPath(Environment.SpecialFolder.System) + "\\drivers\\etc\\hosts";
- string[] hosts = File.ReadAllLines(path);
- List<string> list = hosts.ToList();
- list.RemoveAll(x => x.Contains(domain));
- list.Add($"{ip} {domain}");
- File.WriteAllLines(path, list.ToArray());
- }
- /// <summary>
- /// 删除Hosts记录
- /// </summary>
- /// <param name="hosts_str"></param>
- public static void Remove_Hosts(string hosts_str)
- {
- string path = Environment.GetFolderPath(Environment.SpecialFolder.System) + "\\drivers\\etc\\hosts";
- string[] hosts = File.ReadAllLines(path);
- List<string> list = hosts.ToList();
- list.RemoveAll(x => x.Contains(hosts_str));
- File.WriteAllLines(path, list.ToArray());
- }
- }
复制代码
|
|