1

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.

Sample project

Asteron
  • 93
  • 7
  • When I use OpenIfExists or ReplaceExisting for parameter, I can reproduce your issue. You could try to use `CreationCollisionOption.GenerateUniqueName` to instead, which automatically append a number to the base of the specified name if the folder already exists. – dear_vv Apr 16 '21 at 07:06
  • With `CreationCollisionOption.GenerateUniqueName` it creates a file and can write data to the file, but after that it can not even read from the file `await folder.GetFileAsync("SomeFile")` with the same exception: `UnauthorizedAccessException: 'Access is denied.` – Asteron Apr 16 '21 at 08:08

1 Answers1

0

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.

private async void Button_Click(object sender, RoutedEventArgs e)
        {
            var folder = Windows.Storage.ApplicationData.Current.GetPublisherCacheFolder("SomeFolder");
            try { stream = File.Open(folder.Path + "/SomeFile.txt", FileMode.Open); }
            catch
            {
                file = await folder.CreateFileAsync("SomeFile.txt", CreationCollisionOption.GenerateUniqueName);
                stream = File.Open(folder.Path + "/SomeFile.txt", FileMode.Open);
            }
          
            finally
            {
                var buffer = Encoding.UTF8.GetBytes("SomeContent");
                await stream.WriteAsync(buffer, 0, buffer.Length);
                stream.Dispose();
            }

        }
dear_vv
  • 2,350
  • 1
  • 4
  • 13