/// <summary>
        /// Get the NetworkAdapter Netenabled Property
        /// </summary>
        /// <returns>Whether the NetworkAdapter is enabled</returns>
        public int GetNetConnectionStatus()
        {
            string strWQuery = string.Format("SELECT NetEnabled, NetConnectionStatus FROM Win32_NetworkAdapter "
                                             + "WHERE DeviceID = {0}", DeviceId);

            try
            {
                ManagementObjectCollection networkAdapters =
                    WMIOperation.WMIQuery(strWQuery);
                foreach (ManagementObject networkAdapter in networkAdapters)
                {
                    return(Convert.ToInt32(networkAdapter["NetConnectionStatus"]));
                }
            }
            catch (NullReferenceException)
            {
                // If NullReferenceException return (EnumNetEnabledStatus.Unknow)
            }
            return(0);
        }
        public NetworkAdapter(int deviceId)
        {
            ManagementObject crtNetworkAdapter = new ManagementObject();
            string           strWQuery         = string.Format("SELECT DeviceID, ProductName, "
                                                               + "NetEnabled, NetConnectionStatus "
                                                               + "FROM Win32_NetworkAdapter "
                                                               + "WHERE DeviceID = {0}", deviceId);

            try
            {
                ManagementObjectCollection networkAdapters
                    = WMIOperation.WMIQuery(strWQuery);

                foreach (ManagementObject networkAdapter in networkAdapters)
                {
                    // Expected to be only one ManagementObject instance.
                    crtNetworkAdapter = networkAdapter;
                    break;
                }

                DeviceId   = deviceId;
                Name       = crtNetworkAdapter["ProductName"].ToString();
                NetEnabled = (
                    Convert.ToBoolean(crtNetworkAdapter["NetEnabled"].ToString()))
                                        ? (int)EnumNetEnabledStatus.Enabled
                                        : (int)EnumNetEnabledStatus.Disabled;

                NetConnectionStatus = Convert.ToInt32(
                    crtNetworkAdapter["NetConnectionStatus"].ToString());
            }
            catch (NullReferenceException)
            {
                // If there is no a network adapter which deviceid equates to the argument
                // "deviceId" just to construct a none exists network adapter
                DeviceId            = -1;
                Name                = string.Empty;
                NetEnabled          = 0;
                NetConnectionStatus = -1;
            }
        }
        /// <summary>
        /// List all the NetworkAdapters
        /// </summary>
        /// <returns>The list of all NetworkAdapter of the machine</returns>
        public static List <NetworkAdapter> GetAllNetworkAdapter()
        {
            List <NetworkAdapter> allNetworkAdapter = new List <NetworkAdapter>();

            // Manufacturer <> 'Microsoft'to get all none virtual devices.
            // Because the AdapterType property will be null if the NetworkAdapter is
            // disabled, so we do not use NetworkAdapter = 'Ethernet 802.3' or
            // NetworkAdapter = 'Wireless’
            string strWQuery = "SELECT DeviceID, ProductName, "
                               + "NetEnabled, NetConnectionStatus "
                               + "FROM Win32_NetworkAdapter "
                               + "WHERE Manufacturer <> 'Microsoft'";

            ManagementObjectCollection networkAdapters = WMIOperation.WMIQuery(strWQuery);

            foreach (ManagementObject moNetworkAdapter in networkAdapters)
            {
                try
                {
                    allNetworkAdapter.Add(new NetworkAdapter(
                                              Convert.ToInt32(moNetworkAdapter["DeviceID"].ToString()),
                                              moNetworkAdapter["ProductName"].ToString(),
                                              (Convert.ToBoolean(moNetworkAdapter["NetEnabled"].ToString()))
                                                        ? (int)EnumNetEnabledStatus.Enabled
                                                        : (int)EnumNetEnabledStatus.Disabled,
                                              Convert.ToInt32(moNetworkAdapter["NetConnectionStatus"].ToString()
                                                              )));
                }
                catch (NullReferenceException)
                {
                    // Ignore some other devices (like the bluetooth), that need user
                    // interaction to enable or disable.
                }
            }

            return(allNetworkAdapter);
        }
        /// <summary>
        /// Get the NetworkAdapter Netenabled Property
        /// </summary>
        /// <returns>Whether the NetworkAdapter is enabled</returns>
        public int GetNetEnabled()
        {
            int    netEnabled = (int)EnumNetEnabledStatus.Unknow;
            string strWQuery  = string.Format("SELECT NetEnabled FROM Win32_NetworkAdapter "
                                              + "WHERE DeviceID = {0}", DeviceId);

            try
            {
                ManagementObjectCollection networkAdapters =
                    WMIOperation.WMIQuery(strWQuery);
                foreach (ManagementObject networkAdapter in networkAdapters)
                {
                    netEnabled =
                        (Convert.ToBoolean(networkAdapter["NetEnabled"].ToString()))
                                                                         ? (int)EnumNetEnabledStatus.Enabled
                                                                         : (int)EnumNetEnabledStatus.Disabled;
                }
            }
            catch (NullReferenceException)
            {
                // If NullReferenceException return (EnumNetEnabledStatus.Unknow)
            }
            return(netEnabled);
        }
        /// <summary>
        /// Enable Or Disable The NetworkAdapter
        /// </summary>
        /// <returns>
        /// Whether the NetworkAdapter was enabled or disabled successfully
        /// </returns>
        public int EnableOrDisableNetworkAdapter(string strOperation)
        {
            int resultEnableDisableNetworkAdapter = (int)EnumEnableDisableResult.Unknow;
            ManagementObject crtNetworkAdapter    = new ManagementObject();

            string strWQuery = string.Format("SELECT DeviceID, ProductName, "
                                             + "NetEnabled, NetConnectionStatus "
                                             + "FROM Win32_NetworkAdapter " + "WHERE DeviceID = {0}", DeviceId);

            try
            {
                ManagementObjectCollection networkAdapters =
                    WMIOperation.WMIQuery(strWQuery);
                foreach (ManagementObject networkAdapter in networkAdapters)
                {
                    crtNetworkAdapter = networkAdapter;
                }

                crtNetworkAdapter.InvokeMethod(strOperation, null);

                System.Threading.Thread.Sleep(1000);

                if (strOperation == "Enable")
                {
                    while (GetNetConnectionStatus() == 0)
                    {
                        System.Threading.Thread.Sleep(100);
                    }

                    for (int a = 1; a <= 10; a++)
                    {
                        System.Threading.Thread.Sleep(300);
                    }

                    if (GetNetConnectionStatus() == 2 || GetNetConnectionStatus() == 1)
                    {
                        while (GetNetEnabled() != (int)EnumNetEnabledStatus.Enabled)
                        {
                            System.Threading.Thread.Sleep(100);
                        }
                    }
                }

                if (strOperation == "Disable")
                {
                    while (GetNetConnectionStatus() != 0)
                    {
                        System.Threading.Thread.Sleep(100);
                    }
                }

                resultEnableDisableNetworkAdapter =
                    (int)EnumEnableDisableResult.Success;
            }
            catch (NullReferenceException)
            {
                // If there is a NullReferenceException, the result of the enable or
                // disable network adapter operation will be fail
                resultEnableDisableNetworkAdapter = (int)EnumEnableDisableResult.Fail;
            }

            crtNetworkAdapter.Dispose();

            return(resultEnableDisableNetworkAdapter);
        }