static string hostFilePath = @"C:\Windows\System32\drivers\etc\hosts";
static void Main(string[] args)
Console.WriteLine("Website Blocker App");
Console.WriteLine("\n1. Add Website to Blacklist");
Console.WriteLine("2. Remove Website from Blacklist");
Console.WriteLine("3. List Blocked Websites");
Console.WriteLine("4. Exit");
if (int.TryParse(Console.ReadLine(), out choice))
Console.WriteLine("Invalid choice. Please select a valid option.");
Console.WriteLine("Invalid input. Please enter a number.");
static void AddToBlacklist()
Console.Write("Enter the website to block (e.g., example.com): ");
string website = Console.ReadLine();
if (string.IsNullOrWhiteSpace(website))
Console.WriteLine("Website cannot be empty.");
using (StreamWriter writer = File.AppendText(hostFilePath))
writer.WriteLine($"127.0.0.1 {website}");
Console.WriteLine($"{website} has been added to the blacklist.");
Console.WriteLine($"Error: {e.Message}");
static void RemoveFromBlacklist()
Console.Write("Enter the website to unblock (e.g., example.com): ");
string website = Console.ReadLine();
if (string.IsNullOrWhiteSpace(website))
Console.WriteLine("Website cannot be empty.");
string[] lines = File.ReadAllLines(hostFilePath);
File.WriteAllLines(hostFilePath, lines.Where(line => !line.Contains(website)));
Console.WriteLine($"{website} has been removed from the blacklist.");
Console.WriteLine($"Error: {e.Message}");
static void ListBlockedWebsites()
string[] lines = File.ReadAllLines(hostFilePath);
var blockedWebsites = lines.Where(line => line.Trim().StartsWith("127.0.0.1")).Select(line => line.Split(' ')[1]);
Console.WriteLine("\nBlocked Websites:");
foreach (var website in blockedWebsites)
Console.WriteLine(website);