Click here to Skip to main content
Click here to Skip to main content

Facebook blocker

, 10 Oct 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

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)

Share

About the Author

pavan1361
Student
India India
No Biography provided
Follow on   Google+

Comments and Discussions

 
Question[My vote of 1] Several problems PinmemberGurliGebis10-Oct-12 22:33 
GeneralMy vote of 5 PinmemberDrABELL10-Oct-12 11:38 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 10 Oct 2012
Article Copyright 2012 by pavan1361
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid