How can I block access to certain web pages for Windows 10 system users using Windows Forms in Visual Studio 2019?

kevin david 161 Reputation points
2021-11-22T17:58:17.95+00:00

I'm using Windows Forms to block access to certain web pages for certain Windows users, but I can't find tools to do it, I don't know if it's possible.

Developer technologies Windows Forms
{count} votes

Accepted answer
  1. Jack J Jun 25,296 Reputation points
    2021-11-23T05:53:01.573+00:00

    @kevin david , Based on my research, you could change the hosts file in the path C:\WINDOWS\system32\drivers\etc to block user to access certain web pages.

    You can add the following text to the end of the file.

    For example:

    127.0.0.1 www.google.com  
    

    Also, If you want to use the code to get it, here is a code example you could refer to.

     public partial class Form1 : Form  
        {  
            public Form1()  
            {  
                InitializeComponent();  
            }  
      
            private void button1_Click(object sender, EventArgs e)  
            {  
                string path = @"C:\WINDOWS\system32\drivers\etc\hosts";  
                List<string> list = new List<string>();  
                for (int i = 0; i < comboBox1.Items.Count; i++)  
                {  
                    list.Add("127.0.0.1" + " " + comboBox1.Items[i].ToString());  
                }  
                  
                File.AppendAllLines(path, list);  
                MessageBox.Show("The websites in the combobox has been blocked");  
      
            }  
      
      
            private void Form1_Load(object sender, EventArgs e)  
            {  
                comboBox1.Items.Add("www.google.com");  
                
                comboBox1.Items.Add("stackoverflow.com");  
      
            }  
            private void button2_Click(object sender, EventArgs e)  
            {  
                string path = @"C:\WINDOWS\system32\drivers\etc\hosts";  
                string[] lines = File.ReadAllLines(path);  
                  
                File.WriteAllLines(path, lines.Take(lines.Length - comboBox1.Items.Count).ToArray());  
                MessageBox.Show("The websites in the combobox has been blocked");  
            }  
      
             
        }  
    

    Note: Button1 is used to block websites and Button2 is used to unblock websites. We need to restart the browser to adapt the changes.

    Tested result:

    151662-9.gif

    Hope this could help you.

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.