Skip to content

Commit 8753910

Browse files
committed
Optimized GetProfilesAsync() (#671)
1 parent 20c0fcb commit 8753910

File tree

1 file changed

+33
-22
lines changed

1 file changed

+33
-22
lines changed

InternetTest/InternetTest/Models/WlanProfile.cs

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public override string ToString() => $"{Properties.Resources.Authentication}: {M
114114
$"{Properties.Resources.ConnectionMode}: {(ConnectionMode == "auto" ? Properties.Resources.Automatic : ConnectionMode)}\n" +
115115
$"{Properties.Resources.ConnectionType}: {ConnectionType switch { "ESS" => Properties.Resources.InfrastructureNetwork, "IBSS" => Properties.Resources.AdHocNetwork, _ => ConnectionType }}";
116116

117-
public static async Task<List<WlanProfile>> GetProfilesAsync()
117+
public static async Task<List<WlanProfile>> GetProfilesAsync(bool forceRefresh = false)
118118
{
119119
try
120120
{
@@ -126,32 +126,27 @@ public static async Task<List<WlanProfile>> GetProfilesAsync()
126126
Directory.CreateDirectory(path);
127127
}
128128

129-
// Run "netsh wlan export profile key=clear" command
130-
Process process = new();
131-
process.StartInfo.FileName = "cmd.exe";
132-
process.StartInfo.Arguments = $"/c netsh wlan export profile key=clear folder=\"{path}\"";
133-
process.StartInfo.UseShellExecute = false;
134-
process.StartInfo.CreateNoWindow = true;
135-
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
136-
process.Start();
137-
await process.WaitForExitAsync();
129+
if ((await GetFilesAsync(path, "*.xml")).Length == 0 || forceRefresh)
130+
{
131+
// Run "netsh wlan export profile key=clear" command
132+
Process process = new();
133+
process.StartInfo.FileName = "cmd.exe";
134+
process.StartInfo.Arguments = $"/c netsh wlan export profile key=clear folder=\"{path}\"";
135+
process.StartInfo.UseShellExecute = false;
136+
process.StartInfo.CreateNoWindow = true;
137+
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
138+
process.Start();
139+
await process.WaitForExitAsync();
140+
}
138141

139-
string[] files = Directory.GetFiles(path, "*.xml");
142+
string[] files = await GetFilesAsync(path, "*.xml");
140143

141-
List<WlanProfile> profiles = new();
144+
List<WlanProfile> profiles = [];
142145

143146
for (int i = 0; i < files.Length; i++)
144147
{
145-
XmlSerializer serializer = new(typeof(WlanProfile));
146-
StreamReader streamReader = new(files[i]); // Where the file is going to be read
147-
148-
var profile = (WlanProfile?)serializer.Deserialize(streamReader);
149-
150-
if (profile != null)
151-
{
152-
profiles.Add(profile);
153-
}
154-
streamReader.Close();
148+
var profile = await DeserializeXmlAsync<WlanProfile>(files[i]);
149+
if (profile != null) profiles.Add(profile);
155150
}
156151

157152
return profiles;
@@ -162,4 +157,20 @@ public static async Task<List<WlanProfile>> GetProfilesAsync()
162157
return [];
163158
}
164159
}
160+
161+
private static async Task<string[]> GetFilesAsync(string directory, string searchPatternn)
162+
{
163+
return await Task.Run(() => Directory.GetFiles(directory, searchPatternn));
164+
}
165+
166+
private static async Task<T?> DeserializeXmlAsync<T>(string filepath)
167+
{
168+
return await Task.Run(() =>
169+
{
170+
XmlSerializer serializer = new(typeof(T));
171+
using StreamReader reader = new(filepath);
172+
return (T?)serializer.Deserialize(reader);
173+
});
174+
}
175+
165176
}

0 commit comments

Comments
 (0)