Skip to content

The docmentation: Understanding how packaged desktop apps run on Windows does not apply to LocalCache\Local folder enumeration #497

Description

@HO-COOH

This documentation does not mention about enumerating the LocalCache\Local folder under the package data folder.

When enumerating that folder, it seems to return all the folders (but curiously no files) under %localappdata%, which is extremely confusing and unexpected, and is documented nowhere.

Image

To reproduce:

  1. Create a new packaged winui3 app (C++ in my case)
  2. Add a ListView and add a Loaded handler in the GUI
  3. Use this code
	static std::filesystem::path GetLocalDataFolder()
	{
		wil::unique_cotaskmem_string localAppData;
		SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, NULL, localAppData.put());

		std::array<wchar_t, PACKAGE_FAMILY_NAME_MAX_LENGTH + 1> familyName;
		UINT32 bufferSize = familyName.size();
		GetCurrentPackageFamilyName(&bufferSize, familyName.data());

		return std::filesystem::path{ localAppData.get() } / std::format(LR"(packages\{}\LocalCache\Local)", familyName.data());
	}

	void MainWindow::Files_Loaded(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e)
	{
		std::vector<winrt::Windows::Foundation::IInspectable> result;
		for (auto file : std::filesystem::directory_iterator{ GetLocalDataFolder2() })
		{
			result.push_back(winrt::box_value(file.path().wstring()));
		}
		sender.as<winrt::Microsoft::UI::Xaml::Controls::ListView>().ItemsSource(winrt::single_threaded_vector(std::move(result)));
	}

Using the ApplicationData winrt api gets the same result:

static std::filesystem::path GetLocalDataFolder2()
{
	return std::filesystem::path{ winrt::Windows::Storage::ApplicationData::Current().LocalCacheFolder().Path().c_str() } / L"Local";
}

Repro project here

Additional context: The exact behavior can also be reproduced with a packaged console application. It really is caused by msix virtualization, not winui3

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions