文章

c# 网口切换禁用

  • 注: 在管理员环境下运行才能达到效果
	static ManagementObjectSearcher searcher;
        static ManagementObjectCollection collection;
        // <summary>

        /// 网卡列表

        /// </summary>

        public static List<string> NetWorkList()

        {
            string manage = "SELECT * From Win32_NetworkAdapter";

            searcher = new ManagementObjectSearcher(manage);

            collection = searcher.Get();

            List<string> netWorkList = new List<string>();



            foreach (ManagementObject obj in collection)

            {

                netWorkList.Add(obj["Name"].ToString());
            }
            return netWorkList;

        }

        /// <summary>

        /// 禁用网卡

        /// </summary>5

        /// <param name="netWorkName">网卡名</param>

        /// <returns></returns>

        public static bool DisableNetWork(ManagementObject network)

        {
            if (null == network)
            {
                return false;
            }

            try

            {

                network.InvokeMethod("Disable", null);
                is_dis = true;
                return true;

            }

            catch

            {

                return false;

            }

        }

        /// <summary>

        /// 启用网卡

        /// </summary>

        /// <param name="netWorkName">网卡名</param>

        /// <returns></returns>

        public static bool EnableNetWork(ManagementObject network)
        {

            try

            {

                network.InvokeMethod("Enable", null);
                is_dis = false;
                return true;

            }

            catch

            {

                return false;

            }

        }

        /// <summary>

        /// 网卡状态

        /// </summary>

        /// <param name="netWorkId">网卡名</param>

        /// <returns></returns>

        public static bool NetWorkState(string netWorkName)

        {

            ManagementClass mc = new ManagementClass("Win32_NetworkAdapter");

            ManagementObjectCollection collection = mc.GetInstances();
            bool status = false;
            foreach (ManagementObject manage in collection)

            {
                if (manage["Name"].ToString() == netWorkName)

                {
                    if (manage.GetPropertyValue("Name").ToString() == netWorkName)

                    {
                        //PropertyDataCollection t = mo.Properties;
                        status = (bool)manage.GetPropertyValue("NetEnabled");
                    }
                }
            }
            return status;
        }

        /// <summary>

        /// 得到指定网卡

        /// </summary>

        /// <param name="networkname">网卡名字</param>

        /// <returns></returns>

        public static 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;

        }
License: