Reputation: 11
I am developing a UWP app for the first time. I always used Windows Forms and WPF, but the potential design of these apps is gorgeous, but the problem is they are kind of sandbox, especially for my current project. Basically, I am making a games launcher, where you add your games and the program creates a list of games. (Think of it like a Steam for everything with a modern Windows 10 Material design).
Now, I managed to do almost everything, but two things. First, and most important, is getting the path to the game. I create a file picker, I can pick the .exe files, I can get their names, but the file.Path
property doesn't work, it returns a null path. I thought this might be because of UWP's sandboxed design, but I got far into this, that I really don't want to abandon it.
The second problem would be the launching of these files. I managed to do it on Steam games using Steam's specific URI. ("steam://rungameid/xxxxxx" where xxxxxx is the game id) But it doesn't work if I just put a path as a URI.
What do you think? Are there any solutions? Let me show you some of the related code:
public async void SelectEXE()
{
var picker = new Windows.Storage.Pickers.FileOpenPicker
{
ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail,
SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary
};
picker.FileTypeFilter.Add(".exe");
Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
if (file != null)
{
stuffWrite[0] = file.Name;
stuffWrite[1] = file.Path;
count++;
WriteCount();
WriteItem(count, string.Join(",", stuffWrite));
UpdateList();
}
}
async void LaunchGame(string uriToLaunch)
{
Uri uri = new Uri(uriToLaunch);
games_list.Items.Add(uriToLaunch);
uri = new Uri("steam://rungameid/730"); //replace with actual path
await Windows.System.Launcher.LaunchUriAsync(uri);
}
Upvotes: 1
Views: 1616
Reputation: 1241
Sandbox restrictions make it so that getting the path from that property will not reliably work, as Ahmed pointed out.
As for launching, you can't launch exe files directly from a UWP app. It's simply not possible as it is explicitly restricted from doing so by design. Some workaround solutions to your problem:
Use Steam to launch non-Steam games. You can add non-Steam shortcuts and this creates a SteamID based on the file's path so you can launch it from the UWP app using a URI. I found the algorithm to launch any exe using the Steam URI scheme from this board post and follow the link to the person's Python project source code.
Create a helper Win32 app that does the launching for you. In my app suite I communicate between the UWP and Win32 using WebSocket. There are also new solutions to integrate Win32 apps into UWP like Desktop Bridge and full trust Win32.
Use a 3rd party scripting tool like AutoHotkey. Have your UWP app generate the string of a script to launch the game, and save the text file with extension that launches that script language by default. You can then use the Launcher class to launch the file, which the scripting app will open with by default and then it will launch the game. I doubt your app would be accepted on the Windows Store if you did this.
Consider not using UWP at all. There are some libraries to make WPF look more modern as I'm sure you know.
Upvotes: 2