2015-11-01 98 views
0

我期待編輯hosts在C:\Windows\System32\drivers\etc修改Windows主機文件

我每次啓動檢查程序時

所以文件,如果我想添加(123.123.123.123 download.talesrunner.com),如果它沒有在書面文本hosts文件添加它,並繼續運行代碼,如果它跳過並且不添加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(); 
+1

那麼有什麼問題嗎?你目前的代碼有什麼問題? –

+0

那麼你的代碼有什麼問題?應該有什麼不同? – Kai

+0

我想讓它檢查123.123.123.123 download.talesrunner.com是否存在,如果不存在,它會將它添加到hosts文件中。 [11:24:56 AM] [] Yuki [] MythicalTR:每次我們運行程序時都會添加一個新行。我只想成爲一次。 – Ryan

回答

0

你的問題是這樣的一行:

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

更具體這部分是這只是什麼都不做因爲string.Format中的placehoder缺失:

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

不要忘記,以確保您的程序以管理員權限運行,否則您將得到unauthorized access exception.net。訂單將其更改爲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 }); 
    } 
+0

我收到此錯誤消息:在mscorlib中發生未處理的類型爲'System.ArgumentException'的異常。dll 附加信息:路徑中的非法字符。 – Ryan

+0

@應該在字符串之外。 –

+0

要麼在外面,要麼只是將其取出並在路徑內形成雙重飛濺。我剛剛編輯我的帖子。 – Kai

1

如果要追加的條目只有當它不沒有閱讀存在主機文件,那麼你需要改變你的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);  
+0

我收到與Kai相同的錯誤信息:mscorlib.dll中發生類型'System.ArgumentException'的未處理異常其他信息:路徑中的非法字符 – Ryan

+0

嚴,不知道這裏無效的字符是。你在AppendLines行有沒有遇到異常?您是否在_「... download.talesrunner.com」_後添加空格? – Steve