How to block a website dynamically in C#?
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");
}
}
精彩评论