如何通过代码删除Windows 10 IoT Core的WiFi配置文件以启用SoftAP?
Great question—let’s break this down clearly:
First, the short answer: Yes, the official UWP WiFiAdapter API doesn’t expose a way to delete saved WiFi profiles—it only provides methods like DisconnectAsync() to terminate active connections, as you found in the docs. But that doesn’t mean you’re completely stuck; there are workarounds depending on your scenario.
Alternatives to Delete Saved Profiles (or Work Around the Issue)
Dig deeper into Device Portal API: The WiFi management endpoints you referenced might have an undocumented or less obvious way to delete profiles. While public docs focus on connecting/disconnecting, try sending a
DELETErequest to a profile-specific endpoint (like/api/wifi/profiles/{profileName}) if it exists. Keep in mind this requires the device to have Developer Mode enabled and your app to hold the necessary auth credentials.Use command-line tools (desktop/elevated scenarios): If your app can run with elevated privileges (e.g., a desktop bridge app, or a UWP with a desktop extension), you can call the
netshutility to delete profiles directly. Here’s a quick example:using System.Diagnostics; public async Task DeleteWifiProfile(string profileName) { var processStartInfo = new ProcessStartInfo { FileName = "netsh", Arguments = $"wlan delete profile name=\"{profileName}\"", UseShellExecute = false, RedirectStandardOutput = true, CreateNoWindow = true }; using (var process = Process.Start(processStartInfo)) { await process.WaitForExitAsync(); // Handle output/error messages as needed } }Note: UWP apps don’t have admin rights by default, so you’ll need to prompt the user for elevation or use a companion desktop process to run this.
Skip deletion entirely—toggle SoftAP directly: Depending on your hardware, you might not need to delete profiles to enable SoftAP. Try disconnecting the active WiFi connection first with
DisconnectAsync()or your existing Device Portal code, then attempt to start the SoftAP viaWiFiAdapter.StartSoftApAsync(). Some devices support concurrent WiFi client and SoftAP modes (though this is hardware-dependent).
Your Existing Device Portal Disconnect Code (Formalized)
Just to structure the code you shared for clarity:
// API credentials string username = "your-dev-portal-username"; string password = "your-dev-portal-password"; string deviceIp = "192.168.1.100"; // Replace with your device's IP // Disconnect WiFi using Device Portal API using (HttpClient client = new HttpClient()) { var authToken = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{username}:{password}")); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authToken); var response = await client.PostAsync($"http://{deviceIp}/api/wifi/connect?ssid=&bssid=", null); response.EnsureSuccessStatusCode(); }
In short: The official UWP API won’t let you delete profiles directly, but workarounds exist if you can use elevated tools or explore the Device Portal’s deeper endpoints.
内容的提问来源于stack exchange,提问作者andy

