开发者

How to block a website dynamically in C#?

开发者 https://www.devze.com 2023-02-25 14:11 出处:网络
I would like to block certain URLs dynamically from Windows Service written开发者_StackOverflow中文版 in C#. I don\'t want to do this by writing to hosts file.

I would like to block certain URLs dynamically from Windows Service written开发者_StackOverflow中文版 in C#. I don't want to do this by writing to hosts file.

For example I would like to block the url http://example.com (in all browsers), but also block http://example.com/another from 7 to 8 am.

Is this possible, what should I do?

Best regards, Andrew


You might be able to use the Windows Firewall API. See the article, Managed classes to view/manipulate the Windows Firewall, by John Cole.


Now, I show the concept for block. google website. I hope this solution will help you.

 private void BlockWebsite_Click(object sender, EventArgs e)
    {
        String path = @"C:\Windows\System32\drivers\etc\hosts";
        StreamWriter sw = new StreamWriter(path, true);
        String sitetoblock = "\n 127.0.0.1 google.com";
        sw.Write(sitetoblock);
        sw.Close();
        MessageBox.Show("Site Blocked");
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消