You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

UWP游戏启动器开发疑难:获取EXE路径与外部程序启动失败

Hey there! Making the jump from WinForms/WPF to UWP can throw some curveballs, especially with that sandbox getting in the way. Let's fix those two core issues you're hitting with your Steam-style launcher:

Problem 1: Empty file.Path from FileOpenPicker

This is 100% due to UWP's sandbox restrictions. When you pick a file outside your app's default allowed locations (like the Pictures Library you set as the start location), the Path property will return empty—UWP doesn't let apps directly access arbitrary file system paths for security reasons. Instead, it uses permission tokens to track files you've explicitly been granted access to.

Fix: Use the Future Access List to save file references

Instead of storing the file path, store a persistent access token for the selected file. This lets you retrieve the file later without needing the path, and works around the sandbox limitations.

Here's your updated SelectEXE method:

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) 
    { 
        // Add the file to the Future Access List and get a persistent token
        string accessToken = Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(file);
        
        stuffWrite[0] = file.Name; 
        stuffWrite[1] = accessToken; // Store the token instead of the empty path
        count++; 
        WriteCount(); 
        WriteItem(count, string.Join(",", stuffWrite)); 
        UpdateList(); 
    } 
}
Problem 2: Can't launch programs via file path URIs

LaunchUriAsync only works with URI protocols (like steam:// which Steam registers). Local EXE files aren't URI protocols, so this method won't work for them. You need to use a different approach depending on what you're launching:

Fix 1: Launch files selected via FileOpenPicker

If you're launching an EXE the user picked with the FileOpenPicker (using the token we saved above), use Launcher.LaunchFileAsync() instead. This method respects the sandbox permissions granted via the picker.

Here's an updated LaunchGame method that handles both Steam URIs and local EXEs:

async void LaunchGame(string launchIdentifier) 
{ 
    // Check if we're dealing with a Steam URI
    if (launchIdentifier.StartsWith("steam://")) 
    { 
        Uri steamUri = new Uri(launchIdentifier);
        await Windows.System.Launcher.LaunchUriAsync(steamUri);
    } 
    else 
    { 
        // It's a Future Access List token—retrieve the file and launch it
        try 
        {
            StorageFile gameFile = await Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.GetFileAsync(launchIdentifier);
            bool launchSuccess = await Windows.System.Launcher.LaunchFileAsync(gameFile);
            
            if (!launchSuccess)
            {
                games_list.Items.Add("Failed to launch the game—check if the file still exists!");
            }
        } 
        catch (Exception ex) 
        {
            games_list.Items.Add($"Error launching game: {ex.Message}");
        }
    }
}

Fix 2: Launch arbitrary external EXEs (not selected via picker)

If you need to launch EXEs the user didn't pick with the FileOpenPicker (like system apps or games installed in standard locations), you'll need to use FullTrustProcessLauncher. This requires a small win32 helper app (since UWP can't directly launch arbitrary EXEs) and adding permissions to your package manifest:

  1. Add the runFullTrust capability to your Package.appxmanifest
  2. Declare a windows.fullTrustProcess extension pointing to your helper EXE
  3. Use FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync() to start the helper, which can then launch the target EXE via standard Win32 APIs.

Quick Notes

  • The Future Access List has a default limit of 1000 entries—if your launcher supports lots of games, you might want to implement cleanup for unused tokens.
  • LaunchFileAsync will prompt the user if the EXE requires admin permissions; you can't bypass this from UWP.

内容的提问来源于stack exchange,提问作者SITarabuta

火山引擎 最新活动