/// <summary>
 /// Retorna una lista de adaptadores de red disponibles en la máquina
 /// Si ocurre un error se arroja una Excepción
 /// </summary>
 /// <returns></returns>
 public static List<NetworkAdapter> getNetworkAdapters()
 {
     try
     {
         List<NetworkAdapter> adapters = new List<NetworkAdapter>();
         ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
         ManagementObjectCollection objMOC = objMC.GetInstances();
         foreach (ManagementObject objMO in objMOC)
         {
             if(true)
             {
                 NetworkAdapter adapter = new NetworkAdapter();
                 adapter.Id = (String)objMO["SettingID"];
                 adapter.Description = (String)objMO["Description"];
                 adapter.Index = (UInt32)objMO["Index"];
                 adapters.Add(adapter);
             }
         }
         return adapters;
     }
     catch (ThreadAbortException e)
     {
         throw e;
     }
     catch (Exception e)
     {
         throw e;
     }
 }
        /// <summary>
        /// Busca el adaptador de red WIFI
        /// </summary>
        /// <returns>El adaptador de red Wifi</returns>
        public static NetworkAdapter getWifiAdapter()
        {
            try
            {
                WlanClient wLanClient = new WlanClient();
                foreach (WlanClient.WlanInterface wlanIface in wLanClient.Interfaces)
                {
                    if (wlanIface.NetworkInterface != null)
                    {
                        NetworkAdapter adapter = new NetworkAdapter();
                        adapter.Id = wlanIface.NetworkInterface.Id;
                        adapter.Description = wlanIface.InterfaceDescription;
                        ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
                        ManagementObjectCollection objMOC = objMC.GetInstances();
                        foreach (ManagementObject objMO in objMOC)
                        {
                            String id = (String)objMO["SettingID"];
                            if(adapter.Id.Equals(id))
                            {
                                adapter.Index = (UInt32)objMO["Index"];
                                return adapter;
                            }
                        }

                    }
                }
                return null;
            }
            catch (ThreadAbortException e)
            {
                throw e;
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        /// <summary>
        /// Cambia la Ip del sistema operativo a la configuración de red adhoc, segundo metodo
        /// Si ocurre un error se arroja la excepción
        /// </summary>
        internal static void setStaticIP2(NetworkAdapter networkAdapter, String ip, String subnetMask)
        {
            if (networkAdapter == null)
            {
                throw new Exception("no hay un adaptador de red seleccionado");
            }

            //Invoca los metodos nativos de Windows
            ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection objMOC = objMC.GetInstances();
            foreach (ManagementObject objMO in objMOC)
            {
                if (((UInt32)objMO["Index"]) == networkAdapter.Index)
                {
                    //metodo que pertenecen a Win32_NetworkAdapterConfiguration
                    ManagementBaseObject enableStatic = objMO.GetMethodParameters("EnableStatic");
                    //parametros EnableStatic
                    enableStatic["IPAddress"] = new string[] { ip };
                    enableStatic["SubnetMask"] = new string[] { subnetMask };
                    //ejecuta EnableStatic
                    ManagementBaseObject result = objMO.InvokeMethod("EnableStatic", enableStatic, null);
                    if (((UInt32)result["returnValue"]) != 0)
                    {
                        throw new Exception("EnableStatic: " + getManagementMessage(((UInt32)result["returnValue"])));
                    }

                    break;
                }
            }
        }
 /// <summary>
 /// Cambia la IP del sistema operativo al estádo dinámico
 /// Si ocurre un error se arroja la excepción
 /// </summary>
 internal static void setDinamicIP(NetworkAdapter networkAdapter)
 {
     if (networkAdapter == null)
     {
         throw new Exception("no hay un adaptador de red seleccionado");
     }
     ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
     ManagementObjectCollection objMOC = objMC.GetInstances();
     foreach (ManagementObject objMO in objMOC)
     {
         try
         {
             if (((UInt32)objMO["Index"]) == networkAdapter.Index)
             {
                 //metodo que pertenecen a Win32_NetworkAdapterConfiguration
                 ManagementBaseObject enableDinamic = objMO.GetMethodParameters("EnableDHCP");
                 ManagementBaseObject result = objMO.InvokeMethod("EnableDHCP", enableDinamic, null);
                 if (((UInt32)result["returnValue"]) != 0)
                 {
                     throw new Exception("EnableDHCP: " + getManagementMessage(((UInt32)result["returnValue"])));
                 }
                 break;
             }
         }
         catch (ThreadAbortException e)
         {
             throw e;
         }
         catch (Exception e)
         {
             throw new Exception("error en el adaptador de red: " + objMO["Description"] + ": " + e.Message);
         }
     }
 }
        /// <summary>
        /// Obtiene el numero de bytes enviados por el adaptador
        /// </summary>
        /// <param name="networkAdapter">el adaptador de red</param>
        /// <returns>el numero de bytes enviados</returns>
        internal static bool isOperative(NetworkAdapter networkAdapter)
        {
            bool b = false;

            NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface adapter in adapters)
            {
                if (adapter.Id.Equals(networkAdapter.Id))
                {
                    b = (adapter.OperationalStatus == OperationalStatus.Up);
                    break;
                }
            }
            return b;
        }
        /// <summary>
        /// Obtiene el estado DAD de la Ip asignada a TCP
        /// </summary>
        /// <returns>un estado de la clase IpState</returns>
        internal static int getIpState(NetworkAdapter networkAdapter, IPAddress ipAdress)
        {
            int ipState = IpState.NOTFOUND;

            NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface adapter in adapters)
            {
                if (adapter.Id.Equals(networkAdapter.Id))
                {
                    IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
                    UnicastIPAddressInformationCollection uniCast = adapterProperties.UnicastAddresses;
                    foreach (UnicastIPAddressInformation uni in uniCast)
                    {
                        if (uni.Address.ToString().Equals(ipAdress.ToString()))
                        {
                            switch (uni.DuplicateAddressDetectionState)
                            {
                                case DuplicateAddressDetectionState.Deprecated:
                                    {
                                        ipState = IpState.INVALID;
                                        break;
                                    }
                                case DuplicateAddressDetectionState.Duplicate:
                                    {
                                        ipState = IpState.DUPLICATE;
                                        break;
                                    }
                                case DuplicateAddressDetectionState.Invalid:
                                    {
                                        ipState = IpState.INVALID;
                                        break;
                                    }
                                case DuplicateAddressDetectionState.Preferred:
                                    {
                                        ipState = IpState.VALID;
                                        break;
                                    }
                                case DuplicateAddressDetectionState.Tentative:
                                    {
                                        ipState = IpState.INVALID;
                                        break;
                                    }
                            }
                            break;
                        }
                    }
                    break;
                }
            }
            return ipState;
        }
 /// <summary>
 /// Obtiene el numero de bytes enviados por el adaptador
 /// </summary>
 /// <param name="networkAdapter">el adaptador de red</param>
 /// <returns>el numero de bytes enviados</returns>
 internal static Int64 getAliveValue(NetworkAdapter networkAdapter)
 {
     Int64 n = 0;
     NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
     foreach (NetworkInterface adapter in adapters)
     {
         if (adapter.Id.Equals(networkAdapter.Id))
         {
             n = adapter.GetIPv4Statistics().NonUnicastPacketsReceived;
             break;
         }
     }
     return n;
 }
 /// <summary>
 /// Cambia el estado del adaptador de red a encendido
 /// Si ocurre un error se arroja la excepción
 /// </summary>
 internal static void enableIpAdapter(NetworkAdapter networkAdapter)
 {
     if (networkAdapter == null)
     {
         throw new Exception("no hay un adaptador de red seleccionado");
     }
     ManagementClass objMC = new ManagementClass("Win32_NetworkAdapter");
     ManagementObjectCollection objMOC = objMC.GetInstances();
     foreach (ManagementObject objMO in objMOC)
     {
         try
         {
             if (((UInt32)objMO["Index"]) == networkAdapter.Index)
             {
                 //enable
                 ManagementBaseObject enable = objMO.GetMethodParameters("Enable");
                 ManagementBaseObject resultEnable = objMO.InvokeMethod("Enable", enable, null);
                 if (((UInt32)resultEnable["returnValue"]) != 0)
                 {
                     throw new Exception("EnableNetworkAdapter: " + (UInt32)resultEnable["returnValue"]);
                 }
                 break;
             }
         }
         catch (ThreadAbortException e)
         {
             throw e;
         }
         catch (Exception e)
         {
             throw new Exception("error en el adaptador de red: " + objMO["Description"] + ": " + e.Message);
         }
     }
 }
예제 #9
0
        /// <summary>
        /// Obtiene el estado DAD de la Ip asignada a TCP
        /// </summary>
        /// <returns>un estado de la clase IpState</returns>
        internal static int getIpState(NetworkAdapter networkAdapter, IPAddress ipAdress)
        {
            try
            {
                int ipState = IpState.NOTFOUND;
                INetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
                foreach (INetworkInterface networkInterface in networkInterfaces)
                {
                    if (networkAdapter.Name.Equals(networkInterface.Name))
                    {
                        UnicastIPAddressInformationCollection uaddresses = networkInterface.GetIPProperties().UnicastAddresses;
                        foreach (UnicastIPAddressInformation unicastAddress in uaddresses)
                        {
                            if (ipAdress.ToString().Equals(unicastAddress.Address.ToString()))
                            {
                                switch (unicastAddress.DuplicateAddressDetectionState)
                                {
                                case DuplicateAddressDetectionState.Deprecated:
                                {
                                    ipState = IpState.INVALID;
                                    break;
                                }

                                case DuplicateAddressDetectionState.Duplicate:
                                {
                                    ipState = IpState.DUPLICATE;
                                    break;
                                }

                                case DuplicateAddressDetectionState.Invalid:
                                {
                                    ipState = IpState.INVALID;
                                    break;
                                }

                                case DuplicateAddressDetectionState.Preferred:
                                {
                                    ipState = IpState.VALID;
                                    break;
                                }

                                case DuplicateAddressDetectionState.Tentative:
                                {
                                    ipState = IpState.VALID;
                                    break;
                                }
                                }
                                break;
                            }
                        }
                        break;
                    }
                }
                return(ipState);
            }
            catch (ThreadAbortException e)
            {
                throw e;
            }
            catch (Exception e)
            {
                throw e;
            }
        }