如何在c#中取消阻止网站

时间:2017-06-25 19:17:06

标签: c# .net

拒绝访问路径'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 
  }

1 个答案:

答案 0 :(得分:1)

您的C#正在运行的帐户凭据无权访问您请求的文件夹。你需要:

  • 获取您的帐户凭据访问权限
  • 或使用具有访问权限的其他帐户运行您的代码。

要使用不同的凭据运行您的帐户,请使用 System.Diagnostics.ProcessStartInfo ,或者如果是网站,请更改应用池运行的权限。

你不能绕过安全。如果网站被阻止,您需要为其解锁。代码不会神奇地绕过安全。