/// <summary> /// Refreshes the wifi combo. /// </summary> protected void RefreshWifiCombo(NetworkProfile profile) { WindowsNetworkCard config = profile.NetworkCardInfo; if (config == null) { return; } // get the correct type from dataModel, cause _configuration.CardType is not affidable WindowsNetworkCard wnc = DataModel.FindNetworkCard(config.Id); WindowsNetworkCardType type = WindowsNetworkCardType.UNKNOWN; if (wnc != null) { type = wnc.CardType; } // for wifi card if (WindowsNetworkCardType.WIRELESS == type) { WifiProfile currentWifiProfile = WifiProfileManager.GetActiveWifiProfileForCard(config); List <WifiProfile> listWifiProfile = WifiProfileManager.GetWifiProfilesForCard(config); cbWifiProfile.Items.Clear(); cbWifiProfile.Items.Add(""); foreach (WifiProfile item in listWifiProfile) { cbWifiProfile.Items.Add(item.SSID); } // if nic is disable, no wireless profile found. Add the selected profile if (listWifiProfile.Count == 0) { cbWifiProfile.Items.Add(WifiProfileSSID); } cbWifi.Checked = WifiProfileSelected; cbWifiProfile.Text = WifiProfileSSID; } else { cbWifi.Checked = false; cbWifiProfile.Text = ""; } }
/// <summary> /// Select NIC selected from listview and fill other fields. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> private void SelectNetworkCard() { NetworkProfile profile = Profile; // cerchiamo viewId string work = lstNetworkCard.Text; if (work.Equals(UseCaseProfile.NEW_NIC_NAME)) { } else { int i1 = work.IndexOf("["); if (i1 >= 0) { int i2 = work.IndexOf("]", i1); work = i2 > 0 ? work.Substring(i1, i2 + 1) : work; } if (work.Length > 0) { List <WindowsNetworkCard> lista = DataModel.NetworkCardList; // lo facciamo solo se la card è diversa if (profile.NetworkCardInfo == null || !profile.NetworkCardInfo.ViewId.Equals(work)) { foreach (WindowsNetworkCard item in lista) { if (item.ViewId.Equals(work)) { SelectedNetworkCard = item; if (item.CardType == WindowsNetworkCardType.WIRELESS) { // if present get the active profile WifiProfile wifiProfile = WifiProfileManager.GetActiveWifiProfileForCard(item); if (wifiProfile != null) { Profile.AssociatedWifiSSID = wifiProfile.SSID; wifiProfileControl.WifiProfileSelected = true; wifiProfileControl.WifiProfileSSID = wifiProfile.SSID; wifiProfileControl.DisplayConfiguration(Profile); } } ipControl.DisplayConfiguration(item); dnsConfiguration.DisplayConfiguration(item); macAddressControl.DisplayConfiguration(item); break; } } } } else { SelectedNetworkCard = null; } } DisplaySelectedNetworkCard(false, true); }
/// <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); }