Blog Post

Modern Work App Consult Blog
2 MIN READ

How to launch another app using protocol on .NET Core 3.0 WPF app

KazukiOta's avatar
KazukiOta
Icon for Microsoft rankMicrosoft
Apr 05, 2019

Updated at Apr 22, 2019

Updated to more better workaround written on comments, thank you kodamasakuno .

 

The body of this article

This article is as of .NET Core 3.0 Preview 3.

 

If you write a code in .NET Framework WPF app like as below:

Process.Start("https://example.com"); // Open the URL using default browser

It works fine. However, in .NET Core 3.0, the code occurred following exception:

System.ComponentModel.Win32Exception
  HResult=0x80004005
  Message=The system cannot find the file specified.
  Source=System.Diagnostics.Process

The cause of this issue, it is changing default value in UseShellExecute property at ProcessStartInfo class. .NET Framework is true, but .NET Core is false.

So, the workaround is following code:

Process.Start(new ProcessStartInfo("https://example.com") { UseShellExecute = true });

It works fine.

 

Happy coding!!

 

Old article

In this case, the workaround is to use Launcher.LaunchUriAsync method that is WinRT APIs.

At first, add following references to your project file.

<ItemGroup>
  <PackageReference Include="System.Runtime.WindowsRuntime" Version="4.3.0" />
</ItemGroup>

<ItemGroup>
  <Reference Include="Windows">
    <HintPath>$(MSBuildProgramFiles32)\Windows Kits\10\UnionMetadata\10.0.17763.0\Windows.winmd</HintPath>
    <IsWinMDFile>true</IsWinMDFile>
    <Private>false</Private>
  </Reference>
</ItemGroup>

And then, replace Process.Start to Launcher.LanuchUriAsync.

private async void FooButton_Click(object sender, RoutedEventArgs e)
{
    //Process.Start("https://example.com");
    await Launcher.LaunchUriAsync(new Uri("https://example.com"));
}

It works fine.👍

 

 

Updated Apr 22, 2019
Version 3.0

4 Comments

  • kodamasakuno's avatar
    kodamasakuno
    Copper Contributor

    Here is a better workaround. Just the following line.

    Process.Start(new ProcessStartInfo("https://example.com") { UseShellExecute = true });

    The simple reason is because the default value of ProcessStartInfo.UseShellExecute is false in .Net Core while it's true in .Net Framework.

  • Yes, the workaround can be used on Windows 10.

    On WIndows 7 case, I guess you can use following workaround.

    Process.Start("cmd.exe /c start https://example.com");

    However, It is not valid on Windows 10 S mode(Running on S mode is Microsoft store requirement), because S mode can not use cmd.exe.