任何方式打开"互联网关"在使用C#的窗口? [英] Any way to turn the "internet off" in windows using c#?
问题描述
我要寻找对在C#中的API,让我来控制转动连接和关闭我的Internet连接指针。
I am looking for pointers towards APIs in c# that will allow me to control my Internet connection by turning the connection on and off.
我想要写一个小控制台应用程序,这将让我打开和关闭我的访问,从而提高生产率猛增:)(以及在学习过程中的东西)
I want to write a little console app that will allow me to turn my access on and off , allowing for productivity to skyrocket :) (as well as learning something in the process)
谢谢!
推荐答案
如果你正在使用Windows Vista,您可以使用内置的防火墙,以阻止任何互联网连接。
If you're using Windows Vista you can use the built-in firewall to block any internet access.
以下code创建一个防火墙规则,阻止所有网络适配器的出站连接:
The following code creates a firewall rule that blocks any outgoing connections on all of your network adapters:
using NetFwTypeLib; // Located in FirewallAPI.dll
...
INetFwRule firewallRule = (INetFwRule)Activator.CreateInstance(
Type.GetTypeFromProgID("HNetCfg.FWRule"));
firewallRule.Action = NET_FW_ACTION_.NET_FW_ACTION_BLOCK;
firewallRule.Description = "Used to block all internet access.";
firewallRule.Direction = NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT;
firewallRule.Enabled = true;
firewallRule.InterfaceTypes = "All";
firewallRule.Name = "Block Internet";
INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(
Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
firewallPolicy.Rules.Add(firewallRule);
然后,当你想再次让互联网接入删除的规则:
Then remove the rule when you want to allow internet access again:
INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(
Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
firewallPolicy.Rules.Remove("Block Internet");
这是一些其他的code,我已经使用了轻微的修改,所以我不能作出任何保证它会工作。再次,记住,你需要的Windows Vista(或更高版本)和管理权限进行这个工作。
This is a slight modification of some other code that I’ve used, so I can’t make any guarantees that it’ll work. Once again, keep in mind that you'll need Windows Vista (or later) and administrative privileges for this to work.
链接到防火墙API 文档。
这篇关于任何方式打开"互联网关"在使用C#的窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!