using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using ROOT.CIMV2.Win32;
namespace NetworkRouterRestart
{
class Program
{
static void Main(string[] args)
{
//System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher("root\\CIMV2", "select * from Win32_NetworkAdapter");
//ManagementObjectCollection coll = searcher.Get();
//foreach (ManagementObject obj in coll)
//{
// Console.WriteLine(obj.ClassPath.ClassName);
// string name = obj.Properties["Name"].Value.ToString();
// if (name.Contains("Wireless"))
// obj.InvokeMethod("Disable", null);
//}
//Console.WriteLine("Disabled. Press a key to reenable.");
//Console.ReadLine();
//foreach (ManagementObject obj in coll)
//{
// string name = obj.Properties["Name"].Value.ToString();
// if (name.Contains("Wireless"))
// obj.InvokeMethod("Enable", null);
//}
//Console.WriteLine("Enabled. Press any key to continue...");
//Console.ReadLine();
SelectQuery query = new SelectQuery("Win32_NetworkAdapter", "NetConnectionStatus=2");
ManagementObjectSearcher search = new ManagementObjectSearcher(query);
foreach (ManagementObject result in search.Get())
{
NetworkAdapter adapter = new NetworkAdapter(result);
// Identify the adapter you wish to disable here.
// In particular, check the AdapterType and
// Description properties.
// Here, we're selecting the LAN adapters.
if (adapter.AdapterType.Equals("Ethernet 802.3"))
{
adapter.Disable();
}
}
query = new SelectQuery("Win32_NetworkAdapter", "NetConnectionStatus=0");
search = new ManagementObjectSearcher(query);
foreach (ManagementObject result1 in search.Get())
{
NetworkAdapter adapter1 = new NetworkAdapter(result1);
adapter1.Enable();
}
}
}
}
I got an email last week asking about how to disable a particular network connection under Vista. The specific scenario, how to disable an active 3G connection, is not something I'm going to cover, but what I present below could be used as basis for that scenario.
With Vista, Microsoft introduced two new methods to the Win32_NetworkAdapter class under WMI:Enable and Disable. Before can call either of those methods, we need to know how to enumerate the network connections.
The .NET Framework SDK provides a helpful utility called mgmtclassgen.exe, which can be used to create .NET-friendly wrappers of the WMI classes. Open up a Visual Studio command prompt and enter the following:
mgmtclassgen Win32_NetworkAdapter -p NetworkAdapter.cs
This will generate a file called NetworkAdapter.cs which will contain a C# representation of the WMI Win32_NetworkAdapter class. You can add this source code file to your C# project and then access all the properties without too much extra effort.
To filter and disable the specific adapters, you do something like this:
- SelectQuery query = new SelectQuery("Win32_NetworkAdapter", "NetConnectionStatus=2");
 - ManagementObjectSearcher search = new ManagementObjectSearcher(query);
 - foreach(ManagementObject result in search.Get())
 - {
 - NetworkAdapter adapter = new NetworkAdapter(result);
 - // Identify the adapter you wish to disable here.
 - // In particular, check the AdapterType and
 - // Description properties.
 - // Here, we're selecting the LAN adapters.
 - if (adapter.AdapterType.Equals("Ethernet 802.3"))
 - {
 - adapter.Disable();
 - }
 - }
 
Don't forget to add a reference to System.Management.dll!
| Value | Meaning | 
|---|---|
  | Disconnected  | 
  | Connecting  | 
  | Connected  | 
  | Disconnecting  | 
  | Hardware not present  | 
  | Hardware disabled  | 
  | Hardware malfunction  | 
  | Media disconnected  | 
  | Authenticating  | 
  | Authentication succeeded  | 
  | Authentication failed  | 
  | Invalid address  | 
  | Credentials required  | 
2 comments:
below code is for wireless router
System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher("root\\CIMV2", "select * from Win32_NetworkAdapter");
ManagementObjectCollection coll = searcher.Get();
foreach (ManagementObject obj in coll)
{
Console.WriteLine(obj.ClassPath.ClassName);
string name = obj.Properties["Name"].Value.ToString();
if (name.Contains("Wireless"))
obj.InvokeMethod("Disable", null);
}
Console.WriteLine("Disabled. Press a key to reenable.");
Console.ReadLine();
foreach (ManagementObject obj in coll)
{
string name = obj.Properties["Name"].Value.ToString();
if (name.Contains("Wireless"))
obj.InvokeMethod("Enable", null);
}
Console.WriteLine("Enabled. Press any key to continue...");
Console.ReadLine();
Error occurring in enable and disable method.(OS:Windows XP SP3 and visual studio 2008)
Post a Comment