Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to turn the "internet off" in windows using c#?

Tags:

c#

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)

Thanks !!

like image 777
sundeep Avatar asked Aug 07 '09 02:08

sundeep


People also ask

How do I turn off Internet on Windows?

Go to Start > Control Panel > Network and Internet > Network and Sharing Center. In the left-hand column, click Change adapter settings. A new screen will open with a list of network connections. Right-click Local Area Connection or Wireless Connection and select Disable.

How do I block internet access?

Block App Internet Access Using AndroidTap the menu icon, then “Data usage control”. From here, you'll see a full list of apps, and can tap the dropdowns to the right of each one to control whether you want them connecting online via Wi-Fi only, data only, or not at all.


1 Answers

If you're using Windows Vista you can use the built-in firewall to block any internet access.

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

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.

Link to the firewall API documentation.

like image 118
Greg Avatar answered Oct 14 '22 17:10

Greg