如何在UWP应用中读取系统默认代理设置及指定注册表项值?
Absolutely! But first, a quick heads-up: UWP apps run in a sandbox, so you can't directly read that specific registry key directly. Instead, Microsoft provides official APIs that let you access the system proxy settings properly, which is way more reliable than digging into the registry manually. Let's break down the options:
推荐方法:使用官方网络连接API
UWP offers APIs under the Windows.Networking.Connectivity namespace specifically designed to fetch system network configurations—including proxy settings. This is Microsoft's recommended approach, and it doesn't require extra permissions beyond the basic network access.
Here's a practical code example to retrieve proxy details:
using Windows.Networking.Connectivity; public async Task FetchSystemProxySettings() { // Get the active internet connection profile var activeProfile = NetworkInformation.GetInternetConnectionProfile(); if (activeProfile == null) { // No active internet connection found return; } var proxyConfig = await activeProfile.GetProxyConfigurationAsync(); // Check for auto-config proxy (PAC file) if (!string.IsNullOrWhiteSpace(proxyConfig.AutoConfigurationUri)) { string pacFileUri = proxyConfig.AutoConfigurationUri; // Handle the PAC file address here } // Check for manual proxy settings foreach (var proxyRule in proxyConfig.Proxies) { string proxyHost = proxyRule.Address.HostName; int proxyPort = proxyRule.Address.Port; string applicableRules = proxyRule.Rule; // Process manual proxy details here } }
This API automatically maps to the registry values you mentioned, and it handles dynamic system changes (like a user updating proxy settings) seamlessly—something manual registry reading can't do reliably.
备选方案:通过桌面桥/AppService读取注册表(不推荐)
If you have a very specific need to directly read that registry key, UWP's sandbox restrictions block direct access. In this case, you can use the Desktop Bridge to package your UWP app with a companion desktop process, or use an AppService to let the UWP app call a background desktop process that reads the registry and returns the results.
Here's a sample of the desktop-side code to read the registry:
using Microsoft.Win32; public string GetProxyFromRegistry() { string proxyServer = string.Empty; using (var registryKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings")) { if (registryKey != null) { proxyServer = registryKey.GetValue("ProxyServer") as string; bool isProxyEnabled = (int)registryKey.GetValue("ProxyEnable") == 1; // You can read other related values here too } } return proxyServer; }
Note that this approach requires extra packaging configuration and is less robust than the official API. It should only be used if you have no other option.
Quick Notes
- Don't forget to declare the
Internet (Client)permission in your UWP'sPackage.appxmanifest—without it, network-related APIs will fail. - The official API handles edge cases like proxy exceptions and auto-detection rules automatically, which you'd have to parse manually if reading the registry directly.
内容的提问来源于stack exchange,提问作者Carl

