Click here to Skip to main content
13,000,982 members (59,650 online)
Click here to Skip to main content
Add your own
alternative version


3 bookmarked
Posted 10 Oct 2012

Facebook blocker

, 10 Oct 2012
Rate this:
Please Sign up or sign in to vote.
adding require blocking site in hosts file


Sometimes it becomes ridiculous to edit your hosts file for just adding a list of websites that you want to block  on your machine .... for making a solution and making it easier to do this I have created a program that can edit your hosts file.


For blocking any site you need to add code like at the end of the file and that becomes a ridiculous task to add it whenever we want to block a site and remove it when we want to remove it....

Using the code

We have three buttons: button1 for adding the required blocking site information into the host file and button 2 for removing added code ... and button 3 for exitting from the application, like this:

using System.IO;

//code for button1
private void button1_Click(object sender, EventArgs e)
    using (StreamWriter tw = File.AppendText("C:\\Windows\\System32\\drivers\\etc\\hosts"))

        //TextWriter tw = new StreamWriter("date.txt");

        // write a line of text to the file
       // tw.NewLine();

        // close the stream

//code on button2
private void button2_Click(object sender, EventArgs e)
    TextWriter tw = new StreamWriter("C:\\Windows\\System32\\drivers\\etc\\hosts");
    tw.WriteLine("# Copyright (c) 1993-2009 Microsoft Corp.");
    tw.WriteLine("# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.");
    tw.WriteLine("# This file contains the mappings of IP addresses to host names. Each");
    tw.WriteLine("# entry should be kept on an individual line. The IP address should");
    tw.WriteLine("# be placed in the first column followed by the corresponding host name.");
    tw.WriteLine("# The IP address and the host name should be separated by at least one");
    tw.WriteLine("# space.");
    tw.WriteLine("# Additionally, comments (such as these) may be inserted on individual");
    tw.WriteLine("# lines or following the machine name denoted by a '#' symbol.");
    tw.WriteLine("# For example:");
    tw.WriteLine("#          # source server");
    tw.WriteLine("#              # x client host");
    tw.WriteLine("# localhost name resolution is handled within DNS itself.");
    tw.WriteLine("#       localhost");
    tw.WriteLine("#	::1             localhost");                    
// code for button3
private void button3_Click(object sender, EventArgs e)

Points of Interest

We need to edit file at C:\Windows\System32\drivers\etc\hosts and the host file doesn't have any extension.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

India India
No Biography provided

You may also be interested in...

Comments and Discussions

Question[My vote of 1] Several problems Pin
GurliGebis10-Oct-12 21:33
memberGurliGebis10-Oct-12 21:33 
GeneralMy vote of 5 Pin
DrABELL10-Oct-12 10:38
memberDrABELL10-Oct-12 10:38 
GeneralRe: My vote of 5 Pin
pavan136113-Mar-15 0:22
memberpavan136113-Mar-15 0:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170624.1 | Last Updated 10 Oct 2012
Article Copyright 2012 by pavan1361
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid