/// <summary> /// Gets the current profile. /// </summary> /// <param name="input">The input.</param> /// <returns></returns> internal static WifiProfile GetCurrentProfile(string[] input) { WifiProfile result = null; foreach (string item in input) { if (item.IndexOf("GUID") >= 0) { result = new WifiProfile(); string guid = item.Substring(item.IndexOf(":") + 1).Trim(); result.InterfaceGuid = "{" + guid.ToUpper() + "}"; } if (item.IndexOf("SSID") >= 0 && item.IndexOf("BSSID") == -1) { string ssid = item.Substring(item.IndexOf(":") + 1).Trim(); result.SSID = ssid; } } WindowsNetworkCard card = WindowsNetworkCardManager.RefreshStatus(result.InterfaceGuid); result.InterfaceMAC = card.MacAddress; result.InterfaceDescription = card.Description; result.InterfaceName = card.Name; return(result); }
/// <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); }
/// <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); }
/// <summary> /// Runs the network card setup. /// </summary> /// <param name="profile">The profile.</param> public static void RunNetworkCardSetup(NetworkProfile profile) { WindowsNetworkCardManager.Apply(profile.NetworkCardInfo); }
/// <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); }