|
1 | 1 | using Microsoft.Win32; |
2 | 2 | using System; |
| 3 | +using System.ComponentModel; |
3 | 4 | using System.Diagnostics; |
4 | 5 | using System.IO; |
5 | 6 | using System.Runtime.InteropServices; |
6 | 7 | using System.Text; |
7 | 8 | using System.Threading; |
| 9 | +using System.Windows.Forms; |
8 | 10 |
|
9 | 11 | namespace BluePointLilac.Methods |
10 | 12 | { |
@@ -175,21 +177,37 @@ public static void RestartExplorer() |
175 | 177 | FileName = "explorer.exe", |
176 | 178 | UseShellExecute = true |
177 | 179 | }); |
178 | | - } |
179 | | - |
| 180 | + } |
| 181 | + |
180 | 182 | /// <summary>调用默认浏览器打开指定网址</summary> |
181 | 183 | /// <param name="url">网址</param> |
182 | | - public static void OpenWebUrl(string url) |
183 | | - { |
184 | | - if(url == null) return; |
185 | | - //替换网址转义符 |
186 | | - url = url.Replace("%", "%25").Replace("#", "%23").Replace("&", "%26").Replace("+", "%2B"); |
187 | | - using(Process process = new Process()) |
188 | | - { |
189 | | - //通过explorer来调用默认浏览器打开链接,避免管理员权限影响 |
190 | | - process.StartInfo = new ProcessStartInfo($"\"{url}\""); |
191 | | - process.Start(); |
192 | | - } |
| 184 | + public static void OpenWebUrl(string url) |
| 185 | + { |
| 186 | + if (string.IsNullOrEmpty(url)) return; |
| 187 | + |
| 188 | + try |
| 189 | + { |
| 190 | + // 使用Uri类验证并规范化URL |
| 191 | + Uri uri = new Uri(url); |
| 192 | + using (Process process = new Process()) |
| 193 | + { |
| 194 | + // 显式设置使用Shell执行(关键修复) |
| 195 | + process.StartInfo.UseShellExecute = true; |
| 196 | + // 直接将URL作为文件名,由系统默认浏览器处理 |
| 197 | + process.StartInfo.FileName = uri.AbsoluteUri; |
| 198 | + process.Start(); |
| 199 | + } |
| 200 | + } |
| 201 | + catch (UriFormatException) |
| 202 | + { |
| 203 | + // 处理无效URL格式的情况 |
| 204 | + MessageBoxEx.Show("无效的网址格式:" + url, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 205 | + } |
| 206 | + catch (Win32Exception ex) |
| 207 | + { |
| 208 | + // 处理打开失败的情况 |
| 209 | + MessageBoxEx.Show("无法打开网址:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 210 | + } |
193 | 211 | } |
194 | 212 |
|
195 | 213 | /// <summary>导出指定注册表项的.reg文件</summary> |
|
0 commit comments