-1

How can I disable/enable an internet connection? Just want to disable internet connection only not LAN.

I tried this but it is not working

string[] connections = DisconnectWrapper.Connections();           
for (int i = 0; i < connections.Length; i++)
{
    try
    {
        DisconnectWrapper.CloseConnection(connections[i]);
    }
    catch (Exception ex)
    { }
}
6
  • 2
    Why do you want to do this? Smells bad to me.. Apr 6, 2013 at 15:42
  • @SonerGönül i want to schedule internet connection for specific intervals i am trying it with windows service but how to disable internet connection only that is the problem here...
    – Nighil
    Apr 6, 2013 at 15:44
  • 1
    "not working" is not a sufficient description to help... what exactly happens (error messages/exceptions...) ?
    – Yahia
    Apr 6, 2013 at 15:44
  • 1
    There is no standard way to enable/disable internet connection since there are many ways how the internet connection is made... for example directly, via router, via modem etc. !
    – Yahia
    Apr 6, 2013 at 15:46
  • 2
    Nice of you to tell us you use Internet Access Controller. Why would anyone here know about that software (note to readers: this is not the Microsoft "IAC" product). Apr 6, 2013 at 16:33

2 Answers 2

3

You can use WMI.

Add System.Management to your referenced and try this code

 SelectQuery wmiQuery = new SelectQuery("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionId != NULL");
            ManagementObjectSearcher searchProcedure = new ManagementObjectSearcher(wmiQuery);
            foreach (ManagementObject item in searchProcedure.Get())
            {
                if (((string)item["NetConnectionId"]) == "Local Network Connection")
                {
                    item.InvokeMethod("Disable", null);
                }
            }

There is another article:Disable/Enable Network Connections Programmatically.

with WMI you can disable and enable all network connections.

Edited:

        using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"select * from Win32_NetworkAdapter"))
        {
            ManagementObjectCollection results = searcher.Get();
            foreach (ManagementObject obj in results)
            {
                System.Console.WriteLine("Found adapter {0} :", obj["Caption"]);
                System.Console.WriteLine("Disabling adapter ...");
                object[] param = new object[0];
                obj.InvokeMethod("Disable",param);
                System.Console.WriteLine("Done.");
            }
            Console.ReadLine();
        }

Be aware Some of the adapter cannot be disabled.

4
  • it is not entering in item.InvokeMethod("Disable", null); connection is there
    – Nighil
    Apr 6, 2013 at 16:05
  • @Nighil:there is another way:social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/…
    – KF2
    Apr 6, 2013 at 16:13
  • This method is not implemented in any class in if (((string)item["NetConnectionId"]) == "Local Area Connection") { item.InvokeMethod("Disable", null); }
    – Nighil
    Apr 6, 2013 at 16:27
  • still same error This method is not implemented in any class in the line obj.InvokeMethod("Disable", param);
    – Nighil
    Apr 6, 2013 at 17:37
0

You can do what you need by doing a shell call to netsh.

Example:

netsh interface set interface "Target Adapter Name" enabled
netsh interface set interface "Target Adapter Name" disabled

Netsh will block so if you wait for the process you spawn to exit, you'll know it's done. It requires administrator privileges, so your process will also require it. Netsh will set its exit code to 0 on success or 1 on failure, so you can check that for feedback if needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.