Run External exe from UWP app, workaround sandboxed mode - c#

I have a program where i want to have the ability to launch a local application (for example Spotify) from my UWP App. I have searched the web for a solution as (Process.Start()) doesn't work. As i have understood the UWP applications is kind of sandboxed for saftey and stability reasons. But is there a (simpel) way to work around this.
I only want the ability to start and close a program inside my own frame/Window. No need to interact/send/recive data between my applications and the external program

That is not possible with all apps. Some desktop apps handle protocol launches and that can be a way of launching another app. Spotify actually has a protocol registered so you could do this to launch it:
await Launcher.LaunchUriAsync(new Uri("spotify:"));

I have a program where i want to have the ability to launch a local
application (for example Spotify) from my UWP App.
You can utilize Windows.System.ProcessLauncher API.
Here is a sample about how to launch an external process (exe) from a Universal Windows Platform (UWP) app you can reference.
Make sure add systemManagement capabiity.
For more information reference Process​Launcher.

Related

Open application from Universal Windows(UWP)

So for our project, we have this app that's in the Windows Store. It is a Unity UWP app. At some point, we want to check if program X is installed on the user's machine. If yes, we want to launch it, if no we want to provide the user with a download link (or whatever.)
The app we want to launch, however, is a Unity standalone app. The user can download it from our website.
I tried multiple things. PlayerPrefs, Register checking etc. None of these really work. Register checking turned out to work decently well, but that only works between standalone apps. UWP apps seem to have no access to the Register.
Which left me thinking about URIs etc. But, I'm a complete noob regarding all of this and I have no clue how or where to start. Any help or direction is highly appreciated.
Ideal scenario:
User downloads our app from the Windows Store
Users click a button within our app
Our app checks the user's system for App X
If App X is installed, we launch it, if not we do something else
App X opens.
The UWP app cannot interact with the standalone Unity app directly.
You can write another standalone desktop “helper” app that does the actual work of checking the registry and launching the Unity app, and then make this “helper” app part of the UWP package and resubmit the package to the Windows Store.
Keep the “helper” app as simple as possible so it doesn’t have other dependencies.
Use FullTrustProcessLauncher to launch the helper app when the user clicks the button.
And the UWP app needs to have runFullTrust capability in order to use FullTrustProcessLauncher.
runFullTrust is a restricted capacity and when you submit the app to the Windows Store it is required for you to specify the reason why the capacity is necessary for your app. See this answer for more details.

Detecting programmatically whether a specific UWP app is installed on system

Without going through Windows Registry, is there a quicker way to detect programmatically whether a specific Universal Windows Platform (UWP) app is installed on a system? App will be installed through Windows Apps Store and its installation need to be verified from a Winform program written in C#. But the language doesn't matter.
You should be able to use PackageManager.FindPackage or PackageManager.FindPackageForUser to see if the target package is available universally or for the specific or current user.
See Calling Windows 10 APIs From a Desktop Application for info on how to call this from your WinForms app.
Also check out the Enumerate app packages by user SID sample which demonstrates enumerating app packages from a C# console app. The project used is out of date (it's for VS2013), but the overall code flow should still work.
Depending on your specific scenario (why do you need to know this and what will you do with that information?) there may be better ways for your specific use case. For example: you asked elsewhere about launching a UWP app. If you define and launch a protocol for the app you don't need to check if it's already there first as the protocol launch will offer to look for the app in the store if it's not installed.

How to execute Process commands (or similar) using a Universal Windows Platform (UWP) App?

I'm working on creating custom Cortana commands. The commands are registered and executed using a Universal Windows Platform Application. (GitHub)
For instance, I've registered the following command
<Command Name="ShutDown">
<ListenFor>Shut down</ListenFor>
<Navigate/>
</Command>
To run this function in a UWP application
static async void ShutDown()
{
var dialog = new MessageDialog("This is where I would shut the computer down.");
await dialog.ShowAsync();
//System.Diagnostics.Process.Start("Shutdown", "-s -t 10");
}
But after setting this up I learned System.Diagnostics.Process isn't supported in UWP.
The custom commands I want to run involve some sort of execution such as launching external programs, running other scripts, or opening websites.
It makes sense that UWP doesn't support them given that it's universal and an XBox or a phone might not be able to do these, but I was hoping there was some alternative or hacky way to accomplish this on a Windows 10 PC.
Is there a way for me to execute Process commands or something else with similar functionality in a UWP application? It seems like even though I can get Cortana to execute my C# code, UWP doesn't support much that would be useful in this situation.
Thanks in advance.
There are - limited - ways to achieve similar behavior.
You could use LaunchUri to trigger other apps which registered for a certain URI-Scheme. This should work for your webbrowser scenario. More details here:
https://msdn.microsoft.com/en-us/library/windows/apps/windows.system.launcher.launchuriasync.aspx
You could trigger another app and get results back from it using LaunchForResults. The called app has to support this. More details here:
https://msdn.microsoft.com/en-us/library/windows/apps/mt269386.aspx
You could trigger App Services provided by another app. The called app has to support this. The app service will be executed in background. ( I think this is pretty cool.) More details here:http://blogs.msdn.com/b/mvpawardprogram/archive/2015/06/11/writing-windows-10-app-services-in-javascript.aspx
This is a little hacky: I'm not sure if this still works but it did work for Windows 8.1: You could create a so called "Brokered Component". This allows you to trigger everything from you app on you machine, but you won't be able to publish a brokered component into the store. This also allowed Process.Start() on Windows 8.1. It only worked for sideloaded apps. I'm not sure if it still works on Windows 10.
More info here: https://msdn.microsoft.com/en-us/library/windows/apps/dn630195.aspx
Summary:
Starting another app is pretty easy as long as the target app registered as app service or registered a protocol handler (Uri scheme).
Starting scripts or other *.exe is impossible if option 4 doesn't work any longer.
With the Windows 10 Anniversary Update (1607) there is an option to enable this scenario on PC. With this API in the Desktop Extension SDK you can launch a fulltrust process that runs at the full user privileges:
https://learn.microsoft.com/en-us/uwp/api/Windows.ApplicationModel.FullTrustProcessLauncher
This way you can light it up on the platforms where it is supported, i.e. PCs running 1607 or above. And your app will still be universal:
if (ApiInformation.IsApiContractPresent("Windows.ApplicationModel.FullTrustAppContract", 1, 0))
{
await FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync();
}

Launch Windows Explorer from Metro style app

I would like to download a file to the DownloadsFolder in a Windows Store App. And then I'd like to bring up a Windows Explorer open on the DownloadsFolder (actually on the folder I create in the DownloadsFolder)
But I can't figure out how to do it.
This stackoverflow question Launching a Desktop Application with a Metro-style app suggests using Launcher.LaunchUriAsync. But the documentation claims:
You cannot use this method to launch a URI in the local zone. For example, apps cannot use the file:/// protocol to access files on the local computer. Instead, you must use the Storage APIs to access files.
And indeed, I was trying to use the "file:" protocol to bring up the explorer window. When I did try this mechanism Launcher.LaunchIUriAsync fails.
If the browser can do this, why can't I?
Is there a way for me to bring up windows explorer, or is that outside the real of possibility?
I don't think you can launch the Windows Explorer from metro. One thing you can use, however, is the File Picker.
http://code.msdn.microsoft.com/windowsapps/File-picker-app-extension-0cb95155
If you're willing to have some non-Windows Store components in your solution, there is a workaround for this. Although you can't launch a process directly, you can always run a HTTP listener inside a Windows service which listens for commands from your sandboxed Windows Store (Metro-style) app and launches Explorer (or any other process) for you. A trivial way to do this would be a Web API service inside a Windows service - just implement the GET action in your controller and have arguments for the executable to launch and optionally executable arguments as well.
This is kind of doing an end-run around the sandbox security, though, so you might want to have a tailored Web API instead which just launches a pre-packaged set of apps (like Explorer or one of your own apps).
Of course, for consumer apps this is not a good solution because you can't just install everything from the Windows Store. For LOB apps, though, it's not a bad compromise because you typically have more control over the environment. This is a good way to surface some metrics or other data into a live tile and have your desktop app launch when the tile is clicked. Whether or not this makes for a good user experience is a totally different conversation.
BatRT allows you to run batch file commands from WinRT applications. It utilizes URI calls. This can be used to open up applications or perform file operations.

What's the equivalent of the System.Diagnostic.Process on WinRT (C#)?

I need to launch a couple of commands from my WinRT application, like if it were a Command Console, in order to do this, on not WinRT apps the class to be used is System.Diagnostic.Process but on Win RT his class is not available, is there any equivalent class or method that i could use?
Thanks in advance :)
Windows Store Apps cannot launch other processes directly, as Marylin already said. You can only use Launcher.LaunchFileAsync to launch the default application for the file type (file ending) the passed file has. Using this you could define a self-defined file type like .process in Windows and set its handler application to a windowless desktop application you write. The desktop application reads the process file which has the path to the application stored that is to start and launches it using Process.
This trick would certainly fail the certification but may be useful in apps you deploy to businesses skipping the Store.
A problem would be that the Windows Store App is set to the background if a Desktop application is launched. I think this is one reason that Microsoft does not allow it for certified apps.
You cannot do that from a Windows Store application - those are sandboxed and do not have access to other processes. More details here.

Categories

Resources