示例#1
0
 /// <summary>
 /// Runs the disable network cards setup.
 /// </summary>
 /// <param name="profile">The profile.</param>
 public static void RunDisableNetworkCardsSetup(NetworkProfile profile)
 {
     foreach (WindowsNetworkCard nic in profile.DisabledNetworkCards)
     {
         WindowsNetworkCardHelper.SetDeviceStatus(nic, false);
     }
 }
示例#2
0
        /// <summary>
        /// Sets the status card.
        /// </summary>
        /// <param name="enabled">if set to <c>true</c> [enabled].</param>
        private static void SetStatusCard(bool enabled)
        {
            WindowsNetworkCard ni = DataModel.SelectedNetworkCard;

            if (ni != null && ni.HardwareName.Length > 0)
            {
                String label = enabled ? "Enabled" : "Disabled";

                bool status = WindowsNetworkCardHelper.SetDeviceStatus(ni, enabled);
                UseCaseLogger.ShowInfo(label + " Network Card " + ni.HardwareName + " (" + status + ")");

                RefreshNetworkCardListStatus();
            }
        }
示例#3
0
        /// <summary>
        /// Applies the specified card.
        /// </summary>
        /// <param name="card">The card.</param>
        /// <returns></returns>
        public static bool Apply(WindowsNetworkCard card)
        {
            bool ret = true;

            if (card.HardwareName.Length > 0)
            {
                WindowsNetworkCardHelper.SetDeviceStatus(card, false);
            }
            WindowsNetworkCardManager.WriteDataIntoRegistry(card);
            WindowsNetworkCardManager.WriteWINSbyWMI(card.Id, card.WinsPrimaryServer, card.WinsSecondaryServer);

            if (card.HardwareName.Length > 0)
            {
                WindowsNetworkCardHelper.SetDeviceStatus(card, true);
            }

            return(ret);
        }
示例#4
0
        /// <summary>
        /// Applies the specified card.
        /// </summary>
        /// <param name="card">The card.</param>
        /// <returns></returns>
        public static bool Apply(WindowsNetworkCard card)
        {
            bool ret = true;

            if (card.HardwareName.Length > 0)
            {
                // stop card
                WindowsNetworkCardHelper.SetDeviceStatus(card, false);
            }
            WindowsNetworkCardManager.WriteDataIntoRegistry(card);

            if (card.HardwareName.Length > 0)
            {
                // start card
                WindowsNetworkCardHelper.SetDeviceStatus(card, true);
            }

            return(ret);
        }
示例#5
0
        /// <summary>
        /// Setups the network card for autodetect. Gets the nic associated to profiles and
        /// try to enable it. Then, disable every nic that not have a profile associated.
        /// Use WindowsNetworkCardManager.WindowsNetworkCardList
        /// </summary>
        /// <param name="profiles">The profiles.</param>
        internal static void SetupNetworkCardForAutodetect(List <NetworkProfile> profiles)
        {
            // Get every card and try to enable it
            List <WindowsNetworkCard>    allCardList  = WindowsNetworkCardManager.WindowsNetworkCardList;
            HashSet <WindowsNetworkCard> validCardSet = new HashSet <WindowsNetworkCard>();

            // take only the card with a profile associated
            foreach (WindowsNetworkCard item in allCardList)
            {
                foreach (NetworkProfile itemProfile in profiles)
                {
                    if (item.Id.Equals(itemProfile.NetworkCardInfo.Id) && !validCardSet.Contains(item))
                    {
                        validCardSet.Add(item);
                        NetworkProfileHelper.FireNotifyEvent("Add card " + item.Name + " to enabled card set");
                        break;
                    }
                }
            }

            NetworkProfileHelper.FireNotifyEvent("Found " + validCardSet.Count + " valid card!");
            NetworkProfileHelper.FireNotifyEvent("Now disable every card not in valid set and enable all the card in valid set are disabled");

            // if a nic is used in a profile, we enable it,
            // otherwise disable it
            foreach (WindowsNetworkCard item in allCardList)
            {
                if (item.Enabled && !validCardSet.Contains(item))
                {
                    NetworkProfileHelper.FireNotifyEvent("Disable card " + item.Name + ", it's not in valid set");
                    WindowsNetworkCardHelper.SetDeviceStatus(item, false);
                }
                else if (!item.Enabled && validCardSet.Contains(item))
                {
                    NetworkProfileHelper.FireNotifyEvent("Enable card " + item.Name + " it's in valid set but as disabled");
                    WindowsNetworkCardHelper.SetDeviceStatus(item, true);
                }
            }
            // wait for a while
            NetworkProfileHelper.FireNotifyEvent("Wait " + WAIT_AFTER_SETUP + " ms.");
            System.Threading.Thread.Sleep(WAIT_AFTER_SETUP);
        }
示例#6
0
        /// <summary>
        /// Autodetects the network profile. For the moment it works only for wifi connections!
        /// If no connections found, it return null.
        /// Use
        ///     WindowsNetworkCardManager.EnabledWindowsNetworkCardList
        ///     WifiProfileManager.ActiveWifiProfile;
        /// </summary>
        ///
        /// <param name="profiles">profiles.</param>
        /// <returns></returns>
        public static NetworkProfile AutodetectNetworkProfile(List <NetworkProfile> profiles)
        {
            NetworkProfileHelper.FireNotifyEvent("Start autodetect");
            if (profiles == null || profiles.Count == 0)
            {
                NetworkProfileHelper.FireNotifyEvent("There's no profiles");
                NetworkProfileHelper.FireNotifyEvent("End autodetect");
                return(null);
            }
            // initial setup for cards
            SetupNetworkCardForAutodetect(profiles);

            NetworkProfile selectedProfile = null;

            // one or more card (not only wifi)
            // wifi connected,
            WifiProfile currentWifiProfile = WifiProfileManager.ActiveWifiProfile;

            // enable card
            List <WindowsNetworkCard> enabledCardList = WindowsNetworkCardManager.EnabledWindowsNetworkCardList;

            if (enabledCardList.Count == 1 && enabledCardList[0].CardType == WindowsNetworkCardType.WIRELESS && currentWifiProfile.SSID != null)
            {
                WindowsNetworkCard card = enabledCardList[0];
                // only a wifi connection avaible
                foreach (NetworkProfile item in profiles)
                {
                    // select if card is right and (ssid is right or for that profile there's no ssid)
                    if (item.NetworkCardInfo.Id.Equals(card.Id) && (currentWifiProfile.SSID.Equals(item.AssociatedWifiSSID) || string.IsNullOrWhiteSpace(item.AssociatedWifiSSID)))
                    {
                        // ok, we found it!!!
                        selectedProfile = item;

                        if (!string.IsNullOrWhiteSpace(item.AssociatedWifiSSID))
                        {
                            NetworkProfileHelper.FireNotifyEvent("The card " + card.Name + " are connected with right SSID (" + currentWifiProfile.SSID + ")");
                        }
                        NetworkProfileHelper.FireNotifyEvent("Selected profile " + item.Name + " without do anything else!!");
                        break;
                    }
                }
            }
            else
            {
                // order list by maxSpeed
                //enabledCardList.Sort(CompareCardBySpeed);

                // if there's no enabled card, it's a problem!
                if (enabledCardList.Count == 0)
                {
                    NetworkProfileHelper.FireNotifyEvent("No card enabled, found"); return(null);
                }

                List <NetworkProfile> enabledProfileList = FindValidOrderedNetworkProfiles(profiles, enabledCardList, currentWifiProfile);

                // assert: enabledProfile contains the right profiles. Now we have to test it.
                // the first with ping ok it's ok!
                bool pingOk;
                foreach (NetworkProfile item in enabledProfileList)
                {
                    NetworkProfileHelper.FireNotifyEvent("Start analizing profile " + item.Name + " with card " + item.NetworkCardInfo.Name);
                    //WindowsNetworkCardHelper.SetDeviceStatus(item.NetworkCardInfo, false);
                    NetworkProfileHelper.RunDisableNetworkCardsSetup(item);
                    NetworkProfileHelper.RunNetworkCardSetup(item);

                    // wait for a while
                    //NetworkProfileHelper.FireNotifyEvent("Wait " + (WAIT_BEFORE_PING) + " ms.");
                    //System.Threading.Thread.Sleep(WAIT_BEFORE_PING);
                    WindowsNetworkCardEventHandler eventHandler = new WindowsNetworkCardEventHandler(item.NetworkCardInfo);

                    eventHandler.WaitUntilNetworkCardIsUp();
                    //WaitUntilNetworkCardIsUp(item.NetworkCardInfo);

                    NetworkProfileHelper.FireNotifyEvent("Wait " + (WAIT_BEFORE_PING) + " ms.");
                    System.Threading.Thread.Sleep(WAIT_BEFORE_PING);

                    WindowsNetworkCard card = WindowsNetworkCardManager.RefreshStatus(item.NetworkCardInfo.Id);

                    if (card.CardType == WindowsNetworkCardType.WIRELESS)
                    {
                        // get current wifi profile only for wifi card
                        currentWifiProfile = WifiProfileManager.GetActiveWifiProfileForCard(card);

                        if (currentWifiProfile != null && currentWifiProfile.SSID.Equals(item.AssociatedWifiSSID) && card.NetConnectionStatus == 2)
                        {
                            // ok, we found it!!!
                            selectedProfile = item;
                            NetworkProfileHelper.FireNotifyEvent("The card " + card.Name + " are connected with right SSID (" + currentWifiProfile.SSID + ")");
                            NetworkProfileHelper.FireNotifyEvent("Selected profile " + item.Name + " without do anything else!!");
                            break;
                        }
                    }
                    else
                    {
                        pingOk = false;

                        NetworkProfileHelper.FireNotifyEvent("The card " + card.Name + " are in status " + card.NetConnectionStatus);
                        // test both static config or dynamic config
                        pingOk = PingHelper.RunPing(card.GatewayAddress);
                        pingOk = pingOk || PingHelper.RunPing(card.CurrentGatewayAddress);

                        NetworkProfileHelper.FireNotifyEvent("For card " + card.Name + " and profile " + item.Name + ", ping to gateway are " + pingOk);

                        if (pingOk)
                        {
                            selectedProfile = item;
                            NetworkProfileHelper.FireNotifyEvent("Selected profile " + item.Name + "!!");
                            break;
                        }
                    }

                    NetworkProfileHelper.FireNotifyEvent("Stop analizing profile " + item.Name + ", go to next profile");
                }

                // restore initial enabled card
                if (selectedProfile == null)
                {
                    NetworkProfileHelper.FireNotifyEvent("No profile found, so restore status card");
                    foreach (WindowsNetworkCard item in enabledCardList)
                    {
                        NetworkProfileHelper.FireNotifyEvent("Enable card " + item.Name);
                        WindowsNetworkCardHelper.SetDeviceStatus(item, true);
                    }
                }
            }

            NetworkProfileHelper.FireNotifyEvent("End autodetect");
            return(selectedProfile);
        }