簡體   English   中英

Process.Start 在嘗試打開帶有超鏈接的網站時拋出異常

[英]Process.Start throws an exception when trying to open a website with a hyperlink

此代碼在我的RequestNavigate事件處理程序中引發System.ComponentModel.Win32Exception

Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
e.Handled = true;

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>

您需要將UseShellExecute設置為true

Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri) {UseShellExecute = true});

ProcessStartInfo.FileName的文檔中:

如果 UseShellExecute 為 true,您可以使用 Process 組件啟動任何文檔並對文件執行操作,例如打印。 當 UseShellExecute 為 false 時,您只能使用 Process 組件啟動可執行文件。

UseShellExecute 的默認值UseShellExecute Core 和 .NET 框架中有所不同。

ProcessStartInfo.UseShellExecute 在 .NET Core 上的默認值為 false。 在 .NET 框架上,其默認值為 true。

您還需要使用 processstartinfo 告訴它 shellexecute。

以下是避免此錯誤的工作代碼:

hyper.RequestNavigate += (o, e) => 
Process.Start(new ProcessStartInfo(e.Uri.ToString()) { UseShellExecute = true });

這種行為是最近的變化。

您以前不需要那個 shellexecute,您可能會在 SO 的舊答案中看到不使用它的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM