I have a WPF application packed with desktop bridge package project. WPF project has nuget Microsoft.Windows.SDK.Contracts installed. In the package.appxmanifest I've added the appropriate permission:
<Extensions>
<Extension Category="windows.publisherCacheFolders">
<PublisherCacheFolders>
<Folder Name="SomeFolder" />
</PublisherCacheFolders>
</Extension>
</Extensions>
And I want to share some information between several WPF and UWP applications belonging to one publisher. I call Windows Runtime API:
var folder = Windows.Storage.ApplicationData.Current.GetPublisherCacheFolder("SomeFolder");
var file = await folder.CreateFileAsync("SomeFile", CreationCollisionOption.OpenIfExists);
and I get System.UnauthorizedAccessException: 'Access is denied. Cannot access the specified file or folder (C:\Users<myusername>\AppData\Local\Publishers\5w92pj7ar334m\SomeFolder\SomeFile). Verify that the item is not marked with system or hidden file attributes.'
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at GetPublisherFolderWPF.MainWindow.<MainWindow_Loaded>d__1.MoveNext()
The folders 5w92pj7ar334m and SomeFolder were created after deploying of this app, and the file SomeFile also was created with zero size.
This happens on Windows 2004 and 20H2. On earlier versions of Windows 10 this code works well.
If you want to get the generated file and write it, I suggest you could use File.Open method under System.IO namespace to get filestream, which won't cause permission issues. Please refer to the following code.