UWP全信任进程控制台应用抛出System.IO权限异常咨询
Let's break down your questions and work through this issue step by step.
What is UWP Full Trust Process?
UWP's windows.fullTrustProcess extension lets your UWP app launch a traditional Win32/console executable with user-level full trust permissions—but it’s important to note this doesn’t mean the process is completely free of UWP constraints. Unlike a standalone desktop app, this process runs under the identity of your UWP package, inheriting some sandbox rules. It gets the same access to system resources as the user running the UWP app, but is still bound to the package’s declared capabilities and context limitations.
Why You’re Seeing UnauthorizedAccessException Even With Full Trust
Your console app runs fine on its own, but fails when launched via UWP—here are the most likely causes:
- Default Working Directory Mismatch
When UWP launches the full trust process, the default working directory is set to your UWP app’s read-only installation folder (e.g.,C:\Program Files\WindowsApps\<your-package-id>). IfNayax.exeuses relative paths for file operations (like writing logs or config files), it’s trying to modify the UWP install directory (which you can’t write to). When you run it directly, the working directory isNayax.exe’s own folder, so it has proper write access. - UWP Context Identity Constraints
Even with full trust, the process runs under the UWP package’s identity. Some protected system locations (like core system directories or other apps’ private data folders) might still be off-limits unless your UWP app declares specific capabilities. - Missing UWP Capabilities
If your console app needs access to user-specific locations (Documents, Downloads, external drives), your UWP app must declare the corresponding capabilities in its manifest. The full trust process inherits these permissions from the UWP host.
Fixes & Troubleshooting Steps
Try these solutions to resolve the issue:
- Set the Correct Working Directory in Nayax.exe
Add code at the start of your console app to force its working directory to its own location:
This ensures relative file paths point toSystem.IO.Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);Nayax.exe’s folder instead of the UWP install directory. - Declare Required UWP Capabilities
Add capabilities to your UWP manifest based on the file system locations your app needs access to. For example:<Capabilities> <Capability Name="internetClient" /> <uap:Capability Name="documentsLibrary" /> <uap:Capability Name="downloadsLibrary" /> <!-- Use this only for broad file system access (requires user consent) --> <rescap:Capability Name="broadFileSystemAccess" /> </Capabilities> - Debug the Console App Effectively
To avoid missing the crash due to fast exit, add a delay at the start ofNayax.exeto give you time to attach the debugger:
Alternatively, add logging to a known writable location (like the user’s Downloads folder) to capture exactly which file operation is failing.System.Threading.Thread.Sleep(5000); // Wait 5 seconds before proceeding - Verify All File Paths
Audit file operations inNayax.exe—use absolute paths for critical files, or ensure relative paths are resolved against the app’s own directory (not the UWP context’s default).
Your Existing Setup Reference
For context, your full trust extension declaration is correctly structured:
<Extensions xmlns:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10"> <desktop:Extension Category="windows.fullTrustProcess" Executable="Nayax\Nayax.exe" /> </Extensions>
And your launch code properly checks for the required API contract before execution:
if (ApiInformation.IsApiContractPresent("Windows.ApplicationModel.FullTrustAppContract", 1, 0)) { try { await FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync(); } catch (Exception Ex) { Debug.WriteLine(Ex.ToString()); } }
内容的提问来源于stack exchange,提问作者Adam McMahon

