Search code examples
winui-3appdata

WinUI 3 packaged application not creating folder in AppData


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?


Solution

  • 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.