How to block a website dynamically in C#?

风格不统一 提交于 2019-12-08 02:05:21

问题


I would like to block certain URLs dynamically from Windows Service written 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


回答1:


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




回答2:


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");
    }
}


来源:https://stackoverflow.com/questions/5655008/how-to-block-a-website-dynamically-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!