-1

C# で hostfile を使用すると、Web サイトをブロックできますが、ブロックを解除できませんでした。

String path = @"C:\Windows\System32\drivers\etc\hosts";
StreamWriter sw = new StreamWriter(path, true);
sitetoblock = "\r\n127.0.0.1\t" + txtException.Text;
sw.Write(sitetoblock);
sw.Close();

MessageBox.Show(txtException.Text + " is blocked", "BLOCKED");
lbWebsites.Items.Add(txtException.Text);
txtException.Clear();

ここで、リストボックス (lbWebsites) から選択された特定のサイトのブロックを解除するための助けが必要です。ホストファイルからそれらを削除する方法はありますか? 私は多くのことを試し、他の解決策を見ましたが、すべての解決策で何かがうまくいきません。

4

3 に答える 3

3

サイトをブロックするために書いた行を削除する必要があります。最も効果的な方法は、hosts ファイルを読み込んで再度書き込むことです。

ところで、サイトをブロックする方法はあまり効果的ではありません。あなたの使用シナリオでは問題ないかもしれませんが、少し技術的な人は hosts ファイルを調べることを知っているでしょう。

于 2012-12-21T17:57:22.920 に答える
1

を使用しStreamReaderて、hosts ファイルをstring. 次に、 の新しいインスタンスを初期化して、StreamWriterブロックを解除する Web サイトを除いて、収集されたコンテンツを書き戻します。

string websiteToUnblock = "example.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

ありがとう、
これがお役に立てば幸いです:)

于 2012-12-21T18:01:21.347 に答える