Any Way to Turn the "Internet Off" in Windows Using C#

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

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.

Block Internet connection for Windows 7 using C#

You can use the WMI class Win32_NetworkAdapter to disable/enable the network adapter.

Here's a link to info about that class:

http://msdn.microsoft.com/en-us/library/aa394216%28v=VS.85%29.aspx

Edit: Due to comment said that you only want to disable incoming traffic.

If you want to disable only incoming traffic I think you might have to use the Windows Firewall API, I've never used it so I'm not sure if it would be able to do that but seems plausible, and it is Win 7 compatible as far as I can tell.

Here's a link to it (I'd suggest the VBscript samples might be easier to translate to C# than the C++ samples):

http://msdn.microsoft.com/en-us/library/aa366453%28v=VS.85%29.aspx

Restrict internet access at specified times

You can create a Windows Service which will periodically check the computer time and do whatever tasks you choose accordingly.

Another option is to schedule tasks that will run at certain times.

It seems you already researched how to do this, disabling the LAN or changing the hosts are both ways that will impact the user's ability to use the internet... Disabling the LAN connection will give a full block where as modifying HOST is actually just a blacklist ...

How to Turn off Windows 7 Internet Time Sync Using C#?

I figured it out:

try
{
Registry.SetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Parameters",
"Type", "NoSync", RegistryValueKind.String);
}
catch
{
Console.WriteLine("Could not change internet time sync. Registry error!!!");
}

How to change Global Windows Proxy using C# .NET with `Immediate Effect`

The behavior I want is that when ever
I change proxy settings in my app, all
the browsers which are using global
proxy (irrespective of whether they
are running or not) should instantly
incorporate the change in settings.

How can I achieve this?

You need to refresh your system to achieve that.

Add these lines at the beginning of your code:

using System.Runtime.InteropServices;
using Microsoft.Win32;

Add this in the beginning of your class:

[DllImport("wininet.dll")]
public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);
public const int INTERNET_OPTION_SETTINGS_CHANGED = 39;
public const int INTERNET_OPTION_REFRESH = 37;
static bool settingsReturn, refreshReturn;

And imply the code:

RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
registry.SetValue("ProxyEnable", 1);
registry.SetValue("ProxyServer", YOURPROXY);

// These lines implement the Interface in the beginning of program
// They cause the OS to refresh the settings, causing IP to realy update
settingsReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);
refreshReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);


Related Topics



Leave a reply



Submit