How to use C # to enable a disabled wireless network card

I had a problem when I need to turn on an already disabled card, and the crawler in the WMI NetworkAdapter does not return an object.

I can think of a possible way to do this, but I was not able to get it to work, i.e. create a managementObject using this as the name of the constructor. but it only throws exceptions

{\\.\root\CIMV2:Win32_NetworkAdapter.NetConnectionID='Wireless Network Connection'}

Another way was to shell netsh and turn on the device, which is ugly, or use shell32 / dll "Enable" to do the same, again, both only pass the name. I got the name from a registry scan on HKLM\SYSTEM\CurrentControlSet\Network and looked for MediaType = 2 to get a list of strings of wireless devices. Everything is fine if I launch the application when the adapter is turned on, since I can get the network object for the wireless device, but it all crashes if the application starts when the wireless device is disconnected.

thanks

Edit: this is a code that I would love to work, but not go: (

 using System; using System.Management; class Sample { public static int Main(string[] args) { ManagementObject mObj = new ManagementObject("\\\\.\\root\\CIMV2:Win32_NetworkAdapter.NetConnectionID=\"Wireless Network Connection\""); mObj.InvokeMethod("Enable", null); return 0; } } 
+6
source share
2 answers

This method essentially uses C # to use the WMI and Win32_NetworkAdapter class. It must have built-in methods for:

  • Enable
  • Disable

So, you can execute your command in the selected interface.

You can achieve this as follows:

 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(); } } 

There is a blog that actually describes such a task ; it defines how to create a Wrapper around a WMI class.

Another solution may also use ControlService (advapi32).

 [DllImport("advapi32.dll", SetLastError=true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool ControlService(IntPtr hService, SERVICE_CONTROL dwControl, ref SERVICE_STATUS lpServiceStatus); 

Hope one of these ways helps ..

+1
source
  using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Diagnostics; using System.Security.Principal; namespace WifiRouter { public partial class Form1 : Form { bool connect = false; public Form1() { InitializeComponent(); } public static bool IsAdmin() { WindowsIdentity id = WindowsIdentity.GetCurrent(); WindowsPrincipal p = new WindowsPrincipal(id); return p.IsInRole(WindowsBuiltInRole.Administrator); } public void RestartElevated() { ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.UseShellExecute = true; startInfo.CreateNoWindow = true; startInfo.WorkingDirectory = Environment.CurrentDirectory; startInfo.FileName = System.Windows.Forms.Application.ExecutablePath; startInfo.Verb = "runas"; try { Process p = Process.Start(startInfo); } catch { } System.Windows.Forms.Application.Exit(); } private void button1_Click(object sender, EventArgs e) { string ssid = textBox1.Text, key = textBox2.Text; if (!connect) { if (String.IsNullOrEmpty(textBox1.Text)) { MessageBox.Show("SSID cannot be left blank !", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { if (textBox2.Text == null || textBox2.Text == "") { MessageBox.Show("Key value cannot be left blank !", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { if (key.Length >= 6) { Hotspot(ssid, key, true); textBox1.Enabled = false; textBox2.Enabled = false; button1.Text = "Stop"; connect = true; } else { MessageBox.Show("Key should be more then or Equal to 6 Characters !", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } } else { Hotspot(null, null, false); textBox1.Enabled = true; textBox2.Enabled = true; button1.Text = "Start"; connect = false; } } private void Hotspot(string ssid, string key,bool status) { ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe"); processStartInfo.RedirectStandardInput = true; processStartInfo.RedirectStandardOutput = true; processStartInfo.CreateNoWindow = true; processStartInfo.UseShellExecute = false; Process process = Process.Start(processStartInfo); if (process != null) { if (status) { process.StandardInput.WriteLine("netsh wlan set hostednetwork mode=allow ssid=" + ssid + " key=" + key); process.StandardInput.WriteLine("netsh wlan start hosted network"); process.StandardInput.Close(); } else { process.StandardInput.WriteLine("netsh wlan stop hostednetwork"); process.StandardInput.Close(); } } } private void Form1_Load(object sender, EventArgs e) { if (!IsAdmin()) { RestartElevated(); } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { Hotspot(null, null, false); Application.Exit(); } } } 
0
source

All Articles