0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【.NET 6, .NET Core】デスクトップアプリで既定のブラウザを使ってURLを開く

Last updated at Posted at 2022-07-08

.NET Framework時代

URLをブラウザで開くとき、以下のコードを良く使っていました。

private void btn_Click(object sender, EventArgs e)
{
    Process.Start("https://qiita.com/");
}

.NET Core以降

しかし、.NET Core以降で同じコードを実行すると、ファイルが無いと言われてエラーになってしまいます。
image.png
実行するためには、ProcessStartInfoUseShellExecutetrueに設定する必要があります。

private void btn_Click(object sender, EventArgs e)
{
    Process.Start(new ProcessStartInfo("https://qiita.com/") { UseShellExecute = true });
    // Process.Start(new ProcessStartInfo { FileName = "https://qiita.com/", UseShellExecute = true }); // これでもOK
}

原因

ProcessStartInfo.UseShellExecuteプロパティの既定値が.NET Frameworkではtrue、.NET Core以降ではfalseとなっているため。

The default is true on .NET Framework apps and false on .NET Core apps.

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?