private static async Task <WLANProfileXml.WLANProfile[]> GetWlanProfilesFromDirectoryAsync(string directory = WIFI_PROFILES_BASEDIR) { var parsedprofiles = new List <WLANProfileXml.WLANProfile>(); string[] profiles = Directory.GetFiles(directory, "*.xml", SearchOption.AllDirectories); foreach (string profilepath in profiles) { WLANProfileXml.WLANProfile profile = null; using (var sr = new StreamReader(profilepath)) { profile = await Task.Run(() => (WLANProfileXml.WLANProfile) wlanProfileSerializer.Deserialize(sr)); } if (profile == null) { continue; } if (profile.name == null) { continue; } profile.guid = Path.GetFileNameWithoutExtension(profilepath); parsedprofiles.Add(profile); } return(parsedprofiles.ToArray()); }
private static async Task <WLANProfileXml.WLANProfile[]> GetWlanProfilesFromNetshWithProfileNameAsync(string profileName) { var toret = new WLANProfileXml.WLANProfile[0]; var rnd = new Random(); var tempdir = Path.Combine(Path.GetTempPath(), $"WIFIPASSWORDEXTRACT_{rnd.Next(0, int.MaxValue)}"); Debug.WriteLine(tempdir); if (Directory.Exists(tempdir)) { return(await GetWlanProfilesFromNetshWithProfileNameAsync(profileName)); } try { Directory.CreateDirectory(tempdir); } catch { throw new Exception("Permission Denied"); } try { bool res = await ExportProfileUsingNetshWithClearPasswordAsync(profileName, tempdir); if (res) { toret = await GetWlanProfilesFromDirectoryAsync(tempdir); } } finally { try { Directory.Delete(tempdir, true); } catch { Debug.WriteLine("Failed to remove directory {0}", tempdir); } } return(toret); }