ankush
ankush

Reputation: 1071

how can i block website?

Code:

string path = @"C:\Windows\System32\drivers\etc\hosts";
StreamWriter sw = new StreamWriter(path, true);
string sitetoblock = "\r\n127.0.0.1 http://" + textBox1.Text +
    " 127.0.0.1 http://www." + textBox1.Text;

sw.Write(sitetoblock);
sw.Close();
MessageBox.Show(textBox1.Text + " blocked");

this is a code to block website,....but it is not working... sometime it works.. how can i block website ?

tell me what is right way to block website.

Upvotes: 2

Views: 5214

Answers (3)

Scoregraphic
Scoregraphic

Reputation: 7200

Many Spyware/Adware programs block access to the hosts file. So there is a big chance you may fail doing it this way. Imho it would be better to block the URL in the firewall

Upvotes: 1

Matt H
Matt H

Reputation: 256

The 'hosts' file maps IP addresses to hostnames, so it doesn't need the protocol bits and pieces.

You should be aiming to write "127.0.0.1 hostname" to the file, e.g. to block 'badsite.com' you'd add:

127.0.0.1 badsite.com
127.0.0.1 www.badsite.com

There are probably better ways to block a site though ;-)

Upvotes: 0

Jesse Weigert
Jesse Weigert

Reputation: 4854

First, this is not an effective method to block websites, but if you really want to do it this way, then look up the format of the hosts file.

If you want to block a website by essentially hijacking the DNS lookup, you need to redirect the hostname only.

The hosts file you are generating looks something like this:

127.0.0.1 http://website/file

What you want to see in the file is this:

127.0.0.1 website

Upvotes: 6

Related Questions