【问题标题】:how to unblock website in c#如何在c#中解锁网站
【发布时间】: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 
  }

【问题讨论】:

  • 如果您无法以“正常方式”(使用文件资源管理器)访问文件,则根本不可能访问它,除非您绕过操作系统的安全性。

标签: c# .net


【解决方案1】:

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

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

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

您无法绕过安全措施。如果该网站被阻止,您需要为您解除阻止。代码不会神奇地绕过安全性。

【讨论】:

  • 另外,以管理员身份运行应用程序可能会工作,但我很确定他在这台计算机上没有管理员权限。
  • @Haris 这不是一个解决方案,而是更多的指导。我的解决方案不会为您解除阻止网站,但会回答您为什么拒绝路径的问题。
  • 如何在c#中解锁网站??
  • 这对 Stackoverflow 来说是个坏问题。相反,请尝试解决问题并发布编码问题。
  • @Haris 你不能。该网站因某种原因被您的网络管理员(或您的父母)阻止。
猜你喜欢
  • 1970-01-01
  • 2010-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-14
  • 1970-01-01
  • 2011-02-08
相关资源
最近更新 更多