-1

Using hostfile in C# I can block websites but I couldn't unblock them.

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

Here I need some help to unblock a specific site which is selected from listbox(lbWebsites). Is there a way to remove them from host file? I tried a lot and looked other solutions but something goes wrong in every solution.

Picrofo Software
  • 5,475
  • 3
  • 23
  • 37
user1913674
  • 85
  • 1
  • 7
  • 3
    Of course it's possible. Read the file and remove the IP that you want to unblock and rewrite the file. What does "something goes wrong" even mean? – PhoenixReborn Dec 21 '12 at 17:55
  • Something goes wrong - Write permissions? – fableal Dec 21 '12 at 17:56
  • Blocking sites with the host file is not the elegant way. Just use the built-in windows-firewall to do this ;) Just imagine the case that you have installed a local proxy ;) – Marco Klein Dec 21 '12 at 18:13

3 Answers3

3

You need to remove the lines you wrote to block the site. The most effective way is to read in the hosts file and write it again.

BTW, your method of blocking sites isn't going to be very effective. It might be okay for your usage scenario, but slightly technical people will know to look in the hosts file.

driis
  • 161,458
  • 45
  • 265
  • 341
  • 1
    +1; Windows 8 Smart Screen also aggressively reverts changes to the HOSTS file as well since its so widely used in attacked. – vcsjones Dec 21 '12 at 17:58
1

You may use a StreamReader to read the hosts file into a string. Then, initialize a new instance of a StreamWriter to write the content gathered back excluding the website you want to unblock.

Example

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

Thanks,
I hope you find this helpful :)

Picrofo Software
  • 5,475
  • 3
  • 23
  • 37
  • @user1913674 I'm glad I could help. [Please notice that you may mark a post as an answer to indicate that you have already solved the problem](http://i.stack.imgur.com/uqJeW.png). Have a nice day :) – Picrofo Software Dec 21 '12 at 18:58
  • 1
    +1. Consider using `using(){...}` instead of `.Close` as it safe (`try`/`finally` already written for you - which your sample is missing) and easier to read/verify. – Alexei Levenkov Dec 21 '12 at 19:19
0

You can do this:

String path = @"C:\Windows\System32\drivers\etc\hosts";
System.IO.TextReader reader = new StreamReader(path);
List<String> lines = new List<String>();
while((String line = reader.ReadLine()) != null)
    lines.Add(line);

Then you have all lines of your hosts-file in the lines list. Afterwards, you can search for the site you want to unblock and remove it from the list until the desired site is not anymore in the list:

int index = 0;
while(index != -1)
{
    index = -1;
    for(int i = 0; i< lines.Count(); i++)
    {
        if(lines[i].Contains(sitetounblock))
        {
            index = i;
            break;
        }
    }
    if(index != -1)
        lines.RemoveAt(i);
}

After you have done that, just convert the cleaned list to a normal string:

String content = "";
foreach(String line in lines)
{
    content += line + Environment.NewLine;
}

Then just write content to the file ;)

Written in my head, so no guarantee on having no errors :P

Marco Klein
  • 683
  • 5
  • 19