c#中怎么禁用和开启无线网卡?

我用的是本本.win2008+vs2008下做的程序

其实禁用网卡我已经搞定了,但就是启用不了禁用的无线网卡-_-b,但是普通的物理网卡禁用和启用都没问题!
不能启用无线网卡的原因是无线网卡禁用和开启时的Name不一样.开启的时候我的无线网卡Name是
Intel(R) PRO/Wireless 3945ABG Network Connection #2
但禁用后得到的名字是Intel(R) PRO/Wireless 3945ABG Network Connection
但我在控制面板->网络连接中查看我的无线不论是禁用还是启用时都是Intel(R) PRO/Wireless 3945ABG Network Connection #2名字.
倒底要怎么得到正确的网卡连接名?

Code Snippet

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Management;
using System.Collections;

namespace NetWorkControl
{
   
public partial class Form1 : Form
    {
       
public Form1()
        {
            InitializeComponent();
            NetWorkList();
        }

       
/// <summary>
       
/// 网卡列表
       
/// </summary>
        public void NetWorkList()
        {
           
string manage = "SELECT * From Win32_NetworkAdapter";
            ManagementObjectSearcher searcher
= new ManagementObjectSearcher(manage);
            ManagementObjectCollection collection
= searcher.Get();
            List
<string> netWorkList = new List<string>();

           
foreach (ManagementObject obj in collection)
            {
                netWorkList.Add(obj[
"Name"].ToString());
               
            }
          
this.cmbNetWork.DataSource = netWorkList;

        }

       
/// <summary>
       
/// 禁用网卡
       
/// </summary>5
       
/// <param name="netWorkName">网卡名</param>
       
/// <returns></returns>
        public bool DisableNetWork(ManagementObject network)
        {
           
try
            {
                network.InvokeMethod(
"Disable", null);
               
return true;
            }
           
catch
            {
               
return false;
            }
        }

       
/// <summary>
       
/// 启用网卡
       
/// </summary>
       
/// <param name="netWorkName">网卡名</param>
       
/// <returns></returns>
        public bool EnableNetWork(ManagementObject network)
        {
           
try
            {
                network.InvokeMethod(
"Enable", null );
               
return true;
            }
           
catch
            {
               
return false;
            }
           
        }

       
/// <summary>
       
/// 网卡状态
       
/// </summary>
       
/// <param name="netWorkName">网卡名</param>
       
/// <returns></returns>
        public bool NetWorkState(string netWorkName)
        {
           
string netState = "SELECT * From Win32_NetworkAdapter";
            ManagementObjectSearcher searcher
= new ManagementObjectSearcher(netState);
            ManagementObjectCollection collection
= searcher.Get();
           
foreach (ManagementObject manage in collection)
            {
               
if (manage["Name"].ToString() == netWorkName )
                {
                   
return true;
                }
            }
           
return false;
        }

       
/// <summary>
       
/// 得到指定网卡
       
/// </summary>
       
/// <param name="networkname">网卡名字</param>
       
/// <returns></returns>
        public ManagementObject NetWork(string networkname)
        {
           
string netState = "SELECT * From Win32_NetworkAdapter";

            ManagementObjectSearcher searcher
= new ManagementObjectSearcher(netState);
            ManagementObjectCollection collection
= searcher.Get();

           
foreach (ManagementObject manage in collection)
            {
               
if (manage["Name"].ToString() == networkname)
                {
                   
return manage;
                }
            }

           
           
return null;
        }

       
private void btnStart_Click(object sender, EventArgs e)
        {
           
if (NetWorkState(this.cmbNetWork.SelectedValue.ToString()))
            {
               
if (!EnableNetWork(NetWork(this.cmbNetWork.SelectedValue.ToString())))
                {
                    MessageBox.Show(
"开启网卡失败!");
                }
               
else
                {
                    MessageBox.Show(
"开启网卡成功!");
                }
            }
           
else
            {
                MessageBox.Show(
"网卡己开启!");
            }

            NetWorkList();
        }

       
private void btnStop_Click(object sender, EventArgs e)
        {

           
if (NetWorkState(this.cmbNetWork.SelectedValue.ToString()))
            {
               
if (!DisableNetWork(NetWork(this.cmbNetWork.SelectedValue.ToString())))
                {
                    MessageBox.Show(
"禁用网卡失败!");
                }
               
else
                {
                    MessageBox.Show(
"禁用网卡成功!");
                }
            }
           
else
            {
                MessageBox.Show(
"网卡己禁用!");
            }

            NetWorkList();
        }

    }
}

你可能感兴趣的:(C#)