2

In a WinUI 3 packaged application, I am trying to create a folder in AppData\Local\MyApp. In my application startup I am doing the following:

public App()
{
  string apf = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
  string fp = Path.Combine(apf, "MyApp");
  Directory.CreateDirectory(fp);
}

This does not create any directory in AppData/Local.

When I run this:

public App()
{
  string apf = KnownFolders.GetPath(KnownFolder.AppData);
  string fp = Path.Combine(apf, "MyApp");
  Directory.CreateDirectory(fp);
}

A directory called "MyApp" is created in AppData\LocalLow which is not what I'm looking for. Any thoughts on what I may be doing wrong?

Borisonekenobi
  • 469
  • 4
  • 15
Zen
  • 115
  • 7
  • In fact it does create a directory (here in AppData\Roaming) but it's private to your user and your app https://learn.microsoft.com/en-us/windows/msix/desktop/desktop-to-uwp-behind-the-scenes#appdata-operations-on-windows-10-version-1903-and-later (you can use LocalApplicationData instead but it will be the same, in AppData\Local this time) – Simon Mourier Dec 01 '22 at 07:35

1 Answers1

3

One of the features of MSIX is that it automatically redirects writes to AppData to a private per-user, per-app location during execution.

This means that your directory will actually end up in %localappdata%\Packages\PublisherName.AppName_hash\LocalCache\Roaming.

This behaviour is by design and a requirement to keep the promise of clean uninstalls of apps, i.e. that everything is deleted when you remove the app.

If you really want to write to the "real" non-virtualized %AppData% location from an MSIX packaged app, you must disable the file redirections by editing the application manifest as suggested here. Note that this comes with some limitations and is not recommended.

mm8
  • 163,881
  • 10
  • 57
  • 88