没使用过的关闭网卡C#

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
{
    publicpartialclass Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            NetWorkList();
        }

        ///<summary>
        /// 网卡列表
        ///</summary>
        publicvoid 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>
        publicbool DisableNetWork(ManagementObject network)
        {
            try
            {
                network.InvokeMethod("Disable", null);
                returntrue;
            }
            catch
            {
                returnfalse;
            }
        }

        ///<summary>
        /// 启用网卡
        ///</summary>
        ///<param name="netWorkName">网卡名</param>
        ///<returns></returns>
        publicbool EnableNetWork(ManagementObject network)
        {
            try
            {
                network.InvokeMethod("Enable", null );
                returntrue;
            }
            catch
            {
                returnfalse;
            }
           
        }

        ///<summary>
        /// 网卡状态
        ///</summary>
        ///<param name="netWorkName">网卡名</param>
        ///<returns></returns>
        publicbool 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 )
                {
                    returntrue;
                }
            }
            returnfalse;
        }

        ///<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;
                }
            }

           
            returnnull;
        }

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

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

    }
}
win2003 sp2用不了

posted on 2013-05-22 16:49  Arvin Lai  阅读(214)  评论(0编辑  收藏  举报