Skip to content

Commit 95713ec

Browse files
authored
Merge pull request #42 from LanYun2022/master
修复程序出现的问题
2 parents 470eef3 + ccb74c4 commit 95713ec

File tree

1 file changed

+31
-13
lines changed

1 file changed

+31
-13
lines changed

ContextMenuManager/BluePointLilac.Methods/ExternalProgram.cs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using Microsoft.Win32;
22
using System;
3+
using System.ComponentModel;
34
using System.Diagnostics;
45
using System.IO;
56
using System.Runtime.InteropServices;
67
using System.Text;
78
using System.Threading;
9+
using System.Windows.Forms;
810

911
namespace BluePointLilac.Methods
1012
{
@@ -175,21 +177,37 @@ public static void RestartExplorer()
175177
FileName = "explorer.exe",
176178
UseShellExecute = true
177179
});
178-
}
179-
180+
}
181+
180182
/// <summary>调用默认浏览器打开指定网址</summary>
181183
/// <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+
}
193211
}
194212

195213
/// <summary>导出指定注册表项的.reg文件</summary>

0 commit comments

Comments
 (0)