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

                Thread.Sleep(500);
                while (GetNetEnabled() != ((strOperation.Trim() == "Enable")
                                                    ? (int)EnumNetEnabledStatus.Enabled
                                                    : (int)EnumNetEnabledStatus.Disabled))
                {
                    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);
        }
        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 NetworkAdapter GetNetworkAdapterByDeviceId(int deviceId)
        {
            NetworkAdapter na = null;

            // 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' "
                               + $" and DeviceID = {0}";

            ManagementObjectCollection networkAdapters = WMIOperation.WMIQuery(strWQuery);

            foreach (ManagementObject moNetworkAdapter in networkAdapters)
            {
                try
                {
                    na = 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(na);
        }
        /// <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);
        }