【发布时间】:2017-06-25 19:17:05
【问题描述】:
对路径“C:\Windows\System32\drivers\etc\hosts”的访问被拒绝 c#
try
{
string websiteToUnblock = "youtube.com"; //Initialize a new string of name websiteToUnblock as example.com
StreamReader myReader = new StreamReader(@"C:\Windows\System32\drivers\etc\hosts"); //Initialize a new instance of StreamReader of name myReader to read the hosts file
string myString = myReader.ReadToEnd().Replace(websiteToUnblock, ""); //Replace example.com from the content of the hosts file with an empty string
myReader.Close(); //Close the StreamReader
StreamWriter myWriter = new StreamWriter(@"C:\Windows\System32\drivers\etc\hosts"); //Initialize a new instance of StreamWriter to write to the hosts file; append is set to false as we will overwrite the file with myString
myWriter.Write(myString); //Write myString to the file
myWriter.Close(); //Close the StreamWriter
Console.WriteLine("asas");
}
catch (Exception e)
{
MessageBox.Show(e.Message); // show exception in
}
【问题讨论】:
-
如果您无法以“正常方式”(使用文件资源管理器)访问文件,则根本不可能访问它,除非您绕过操作系统的安全性。