6

I am trying to run a Windows Universal App from my winform using the following code but unfortunately it opens the documents folder. I am new in UWP app development. Is it the correct way to launch a UWP app?

Process p = new Process();
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = "explorer.exe";
            startInfo.Arguments = @"shell:appsFolder\Microsoft.SDKSamples.CameraAdvancedCapture.CS_8wekyb3d8bbwe!App";
            p.StartInfo = startInfo;
            p.Start();

3 Answers 3

10

You really have two questions here:

  1. How do you launch a protocol from a WinForms app
  2. How to properly launch a UWP app.

To launch a protocol from your WinForms app use the Process object with UseShellExecute = true. Don't try launching it with Explorer.exe as the process.

The best way to launch an app is via protocol, so long as the app defines one. If you control the app then you can define a protocol as shown by @Romasz: Handle URI activation

The shell:appsFolder trick you used on your command line is a handy scripting hack, but it's not documented or guaranteed. Don't ship code dependent on it.

Once you have a protocol you can launch it with Process.Start:

Here's the shell hack to launch the People app:

Process p = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.FileName =  startInfo.FileName =  @"shell:appsFolder\Microsoft.People_8wekyb3d8bbwe!App";
p.StartInfo = startInfo;
p.Start();

Since the People app defines a documented protocol it'd be better to launch it that way. This can also let us choose which contact we want:

Process p = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.FileName = @"ms-people:viewcontact?PhoneNumber=8675309";
p.StartInfo = startInfo;
p.Start();

The correct way to launch a UWP app that doesn't define a protocol is to use the IApplicationActivationManager. This is what the shell will use internally, and it can give you more control over what you're launching and how.

There is a stackoverflow Q/A on using IApplicationActivationManager from C# at IApplicationActivationManager::ActivateApplication in C#?

1
  • 2
    will this work if Powershell is blocked in the machine for the current user and available to admin users only?
    – Ali123
    Feb 4, 2020 at 5:44
2

You can use Automate launching Windows 10 UWP apps

"C:\Program Files (x86)\Windows Kits\10\App Certification Kit\microsoft.windows.softwarelogo.appxlauncher.exe" MyPackageName_ph1m9x8skttmg!AppId

Where MyPackageName_ph1m9x8skttmg is the Package family name of your UWP app and AppId is the Application Id in your Package.appxmanifest file under <Applications> xml element.

2
  • 1
    Windows Kits is available for those who installed Windows SDK for development. It's not showing on non-developer laptop
    – Ali123
    Feb 4, 2020 at 5:47
  • This command saved my day, I struggled to get the app id though. we can get it from the UWP projects Package.apxmanifest -> Package tab -> Package Family Name. Or run the power-shell command get-StartApps | Where-Object {$_.Name -like 'appname'}
    – Varun
    May 5, 2021 at 10:42
0

You can register the app for the URI scheme and then activate it. Inside the app you will have to handle OnActivated event.

As for some more information this MSDN page and this SO answer can be helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.