WINUI3 Packaged App: Copy folders/files during installation locally

43 Views Asked by At

I have the following problem. I have a packages winui program which should open a CANoe config when a button is pressed. Behind this CANoe config there are other SubFolder/Files which are also needed.

So that I don't have to put these files on a network drive, I wanted the tool to store these files locally on the respective computer during installation and always open them from there.

Example folder structure which should be copied locally

  • Folder 1
    • Subfolder 1
      • files
    • Subfolder 2
      • files
    • Subfolder 3
      • files

How do I ensure that this folder structure is copied to the following path of my computer during installation via the msxi bundle?

AppData\Local\Packages\{your app's ID}\temp\Folder 1

...........................................

2

There are 2 best solutions below

2
Jeaninez - MSFT On

There is no way to copy this folder structure to the specified path of the computer during installation via the MSIX bundle.

When you double click the .MSIX (during installation), App Installer launches and provides the basic app information as well as an install button, installation progress bar, and any relevant error messages.

0
Andrew KeepCoding On

As an alternative, you can create the folders/files when the app runs.

For example:

  1. Create a "Folder 1" on your project.
  2. Inside "Folder 1", create "Subfolder 1".
  3. Inside "Subfolder 1", create a "TextFile1.txt" file.
  4. Set "TextFile1.txt"'s Build Action to Content.
  5. Implement the next code in App.xaml.cs:
public partial class App : Application
{
    private Window _window;

    public App()
    {
        this.InitializeComponent();
    }

    protected override async void OnLaunched(LaunchActivatedEventArgs args)
    {
        await CreateAppFolders();
        _window = new MainWindow();
        _window.Activate();
    }

    private static async Task CreateAppFolders()
    {
        StorageFolder temporaryFolder = ApplicationData.Current.TemporaryFolder;
        IReadOnlyList<StorageFolder> temporaryFolderSubFolders = await temporaryFolder.GetFoldersAsync();

        // Returns if "Folder 1" already exists.
        if (temporaryFolderSubFolders.FirstOrDefault(folder => folder.Name == "Folder 1") is StorageFolder folder1)
        {
            return;
        }

        System.Diagnostics.Debug.WriteLine($"Creating 'Folder 1' and its subfolders on {temporaryFolder.Path}");

        folder1 = await temporaryFolder.CreateFolderAsync("Folder 1", CreationCollisionOption.OpenIfExists);

        StorageFolder subFolder1 = await folder1.CreateFolderAsync("SubFolder 1", CreationCollisionOption.OpenIfExists);
        StorageFile textFile1 = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Folder 1/SubFolder 1/TextFile1.txt"));
        await textFile1.CopyAsync(subFolder1, "TextFile1.txt", NameCollisionOption.ReplaceExisting);

        StorageFolder subFolder2 = await folder1.CreateFolderAsync("SubFolder 2", CreationCollisionOption.OpenIfExists);
        StorageFolder subFolder3 = await folder1.CreateFolderAsync("SubFolder 3", CreationCollisionOption.OpenIfExists);
    }
}