修改 windows 主机文件

Modifying windows hosts file

我想在 C:\Windows\System32\drivers\etc

编辑主机文件

所以每次我启动程序时,它都会检查我是否要添加文本 (123.123.123.123 download.talesrunner.com),如果它没有写在主机文件中,它会添加它并继续 运行代码,如果是,它会跳过并且不添加 123.123.123.123 download.talesrunner.com 并继续 运行 代码。

所以我想这样做

这是代码:

var hostfile = "";
Console.ForegroundColor = ConsoleColor.Red;
var OSInfo = Environment.OSVersion;
if (OSInfo.Platform == PlatformID.Win32NT)
{
    //is windows NT
    HOSTFILE = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), @"system32\drivers\etc\hosts");
}
else
{
    //is no windows NT
    HOSTFILE = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "hosts");
}
Console.WriteLine(HOSTFILE);
IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
var myIP = host.AddressList.FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork).ToString();
File.AppendAllLines(HOSTFILE, new[] {string.Format("123.123.123.123 download.talesrunner.com", myIP) });    
Console.ReadLine();
InitializeComponent();

你的问题是这一行:

File.AppendAllLines(hostfile, new[] { string.Format("123.123.123.123 download.talesrunner.com", myIP) });

更具体地说,这部分什么都不做,因为 string.Format 中的占位符丢失了:

 new[] { string.Format("123.123.123.123 download.talesrunner.com", myIP) }

如果您真的只想将 123.123.123.123 download.talesrunner.com 添加到文件中(如果它不存在),我会这样做:

const string tales = "123.123.123.123 download.talesrunner.com";
if (!File.ReadAllLines(hostfile).Contains(tales))
{
     File.AppendAllLines(hostfile, new String[] { tales });
}

整个代码:

var OSInfo = Environment.OSVersion;
string pathpart = "hosts";
if (OSInfo.Platform == PlatformID.Win32NT)
{
    //is windows NT
    pathpart = "system32\drivers\etc\hosts";
}
string hostfile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), pathpart);

const string tales = "123.123.123.123 download.talesrunner.com";
if (!File.ReadAllLines(hostfile).Contains(tales))
{
    File.AppendAllLines(hostfile, new String[] { tales });
}

不要忘记确保您的程序以管理员权限运行,否则您将获得 .netunauthorized access exception

第二次编辑 以便将其更改为 123.123.123.123 如果 download.talesrunner.com 已经在具有不同 ip 的文件中:

    const string tales = "123.123.123.123 download.talesrunner.com";
    string[] lines = File.ReadAllLines(hostfile);

    if (lines.Any(s => s.Contains("download.talesrunner.com")))
    {
        for (int i = 0; i < lines.Length; i++)
        {
             if (lines[i].Contains("download.talesrunner.com"))
                 lines[i] = tales;
        }
        File.WriteAllLines(hostfile, lines);
    }
    else if (!lines.Contains(tales))
    {
        File.AppendAllLines(hostfile, new String[] { tales });
    }

如果您只想在不读取主机文件的情况下附加您的条目​​,那么您需要更改您的 Dns 查询

IPHostEntry host = Dns.GetHostEntry("download.talesrunner.com");
if (host != null)
{
    bool hasEntry = false;
    foreach (IPAddress ip in host.AddressList)
    {
        if (ip.ToString() == "123.123.123.123")
        {
            hasEntry = true;
            break;
        }
    }
    if(!hasEntry)
        File.AppendAllLines(..., 
}

那你写条目的代码好像不正确。 hosts 文件中的条目由一个 IP 地址和后跟一系列由 space 或制表符分隔的主机名组成,因此,如果你想将每个对 "download.talesrunner.com" 的调用重定向到 IP 地址“123.123 .123.123" 和您的 IP 地址,然后您需要添加

string[] entry = new string[] 
         { "123.123.123.123 download.talesrunner.com " + MyIp };
File.AppendAllLines(HOSTFILE, entry);