尝试打开带有超链接的网站时 Process.Start 引发异常

Muk*_*yev 3 c# wpf

此代码System.ComponentModel.Win32Exception在我的RequestNavigate事件处理程序中抛出一个。

Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
e.Handled = true;
Run Code Online (Sandbox Code Playgroud)

Visual Studio 中的异常

这是我的 XAML 代码:

<TextBlock Margin="0 0 40 40" FontSize="13" HorizontalAlignment="Right" VerticalAlignment="Bottom">
   <Hyperlink Foreground="Snow" RequestNavigate="Hyperlink_RequestNavigate" NavigateUri="http://www.google.com">Bizning jamoa bilan tanishishni</Hyperlink>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)

tha*_*guy 7

您需要设置UseShellExecutetrue.

Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri) {UseShellExecute = true});
Run Code Online (Sandbox Code Playgroud)

来自以下文档ProcessStartInfo.FileName

如果 UseShellExecute 为 true,则可以使用 Process 组件启动任何文档并对文件执行操作,例如打印。当 UseShellExecute 为 false 时,您只能使用 Process 组件启动可执行文件。

.NET Core 和 .NET Framework 中的默认值有所不同。UseShellExecute

ProcessStartInfo.UseShellExecute 在 .NET Core 上的默认值为 false。在 .NET Framework 上,其默认值为 true。