C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Windows applicaton to block a website


Posted Date: 27 Jun 2009    Resource Type: Articles    Category: .NET Framework
Author: ABitSmartMember Level: Diamond    
Rating: 1 out of 5Points: 5 (Rs 5)



Think in the lines of Parental control application where we need to block certain websites from being accessed. To make it harder, this will be a Windows application where user can configure the sites to be blocked and unblocked.

Let's start thinking of what we can do to achieve this.

Should we monitor the incoming/outgoing packets to intercept the source/destination ? Wow, that would be fun, interesting - Hacking! No we should not do this or is this even possible?. Let's see the other options.

Should we intercept the browser object and then check the URL being used ? We can but not a full proof method. We will have to continuously check for the browser to see if a Blocked URL is being accessed. Not at all an option.

So, what do we do ?

Don't panic. A simple approach can be used to achieve this.

There is a file "Network host file" present on the system. This file contains DNS Maps for the system used by Windows. It can be found on your machine at for example, "C:\WINDOWS\SYSTEM32\DRIVERS\ETC\HOSTS" (i.e. %SystemRoot%\System32\Drivers\etc). By default this file will contain an entry like this,

127.0.0.1 localhost

This entry means, resolve localhost to 127.0.0.1.

The way to block website is to add a new entry to this list like,

127.0.0.1 www.WebsiteToBeBlocked.com

Note: This is a windows file so it might be readonly and might need permissions.
By adding the entry we are basically looping the call www.WebsiteToBeBlocked.com back to local machine and since the local machine won't be a webserver this call will fail.

So the windows application for Parental control will just have to open the file for editing and add the particular sites entry at the end of it i.e. append the entry.

//demonstration code
string hostfile = "C:\\Windows\\System32\\drivers\\etc\\hosts"; //hardcoding for demonstration purpose
string newEntry = Environment.NewLine + "127.0.0.1 theSiteToBlock.com"; //hardcoding for demonstration purpose

StreamWriter writer = new StreamWriter(hostfile, true); //The second parameter is true to open the file in append mode.
writer.Write(newEntry);
writer.Close();


To unblock the site, delete its corresponding entry from the file.

Have fun.

For more details, visit http://abitsmart.com/?p=117



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Windows application to block a website  .  C# block a website  .  Block a website  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: SQL Server Interview Questions
Previous Resource: Find if database, table or stored procedure exists using C#
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use