正在修改windows主机文件

本文关键字:主机 文件 windows 修改 | 更新日期: 2023-09-27 18:20:09

我想在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();

正在修改windows主机文件

您的问题是这一行:

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 });
}

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

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

    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(..., 
}

那么,编写条目的代码似乎不正确。主机文件中的一个条目由一个IP地址和一系列主机名组成,主机名由空格或制表符分隔,因此,如果您想将每次对"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);