Click here to Skip to main content
15,885,757 members
Articles / Desktop Programming / Win32
Tip/Trick

Facebook blocker

Rate me:
Please Sign up or sign in to vote.
4.56/5 (2 votes)
10 Oct 2012CPOL 11.5K   126   4   3
adding require blocking site in hosts file

Introduction 

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.

Background

For blocking any site you need to add code like 127.0.0.1 http://www.facebook.com 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:

C#
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();
        tw.WriteLine("127.0.0.1 login.facebook.com");
        tw.WriteLine("127.0.0.1 www.facebook.com");
        tw.WriteLine("127.0.0.1 blog.facebook.com");
        tw.WriteLine("127.0.0.1 apps.facebook.com");
        tw.WriteLine("");

        // close the stream
        tw.Close();
    }
}

//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("#");
    tw.WriteLine("# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.");
    tw.WriteLine("#");
    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("#");
    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("#");
    tw.WriteLine("# For example:");
    tw.WriteLine("#");
    tw.WriteLine("#      102.54.94.97     rhino.acme.com          # source server");
    tw.WriteLine("#       38.25.63.10     x.acme.com              # x client host");
    tw.WriteLine("# localhost name resolution is handled within DNS itself.");
    tw.WriteLine("#	127.0.0.1       localhost");
    tw.WriteLine("#	::1             localhost");                    
    tw.WriteLine("");
    tw.Close();
    tw.Dispose();
}
// code for button3
private void button3_Click(object sender, EventArgs e)
{
    Application.Exit();
}

Points of Interest

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

License

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


Written By
Student
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Question[My vote of 1] Several problems Pin
GurliGebis10-Oct-12 21:33
GurliGebis10-Oct-12 21:33 
GeneralMy vote of 5 Pin
DrABELL10-Oct-12 10:38
DrABELL10-Oct-12 10:38 
GeneralRe: My vote of 5 Pin
pavan136113-Mar-15 0:22
pavan136113-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.