I have a WINUI 3 application in which I have add multiple of my own text files/photos. However, when I package the app and download the exe through the package, it tries to load those files from C:\Program Files\WindowsApps\PackageName\FilePath
. Which rightfully so gives it access denied error.
Code:
public static string ProjBaseDirectory = System.AppDomain.CurrentDomain.BaseDirectory;
public static string CountriesListPath = Path.Combine(ProjBaseDirectory, @"Assets\Contents\Docs\Countries.txt");
Above is how I try to access most of the files, and the path I get from that when debugging is C:\user\Projects\iAssist\iAssist\bin\x64\Debug\net6.0-windows10.0.19041.0\win10-x64\AppX\Assets\Contents\Docs\Countries.txt
But the same line gives me the path to C:\Program Files\WindowsApps\PackageName\FilePath
when packaging it.
Properties: Other files/images properties mimic this one.
I have been trying out multiple solution like trying different options for Build Action for the properties of the files, but nothing seems to work. Is there any work around this problem, any help would be highly appreciated, I have been stuck on this problem for two days now.
Thank You!
Let me show you an example based on @Simon Mourier comment about Windows.Storage.ApplicationData
.
In this example, I added a text file, SomeText.txt, to the Assets folder in the project.
MainPage.xaml
<Page
x:Class="WindowsStorageApplicationDataExample.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:WindowsStorageApplicationDataExample"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
mc:Ignorable="d">
<Grid RowDefinitions="Auto,Auto,*">
<Button
Grid.Row="0"
Click="InitializeButton_Click"
Content="Initialize" />
<Button
Grid.Row="1"
Click="UpdateSomeTextButton_Click"
Content="Update SomeText.txt" />
<ItemsRepeater
Grid.Row="2"
ItemsSource="{x:Bind Logs}" />
</Grid>
</Page>
MainPage.xaml.cs
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System;
using System.Collections.ObjectModel;
using Windows.ApplicationModel;
using Windows.Storage;
namespace WindowsStorageApplicationDataExample;
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
public ObservableCollection<string> Logs { get; } = new();
private StorageFile? SomeTextInLocalStateFolderFile { get; set; }
private async void InitializeButton_Click(object sender, RoutedEventArgs e)
{
StorageFolder installedLocationFolder = Package.Current.InstalledLocation;
Logs.Add($"InstalledLocation: {installedLocationFolder.Path}");
StorageFolder assetsFolder = await installedLocationFolder.GetFolderAsync("Assets");
Logs.Add($"Assets folder: {assetsFolder.Path}");
StorageFile someTextFile = await assetsFolder.GetFileAsync("SomeText.txt");
Logs.Add($"SomeText.txt file: {someTextFile.Path}");
StorageFolder localStateFolder = ApplicationData.Current.LocalFolder;
Logs.Add($"LocalState folder: {localStateFolder.Path}");
if (await localStateFolder.TryGetItemAsync("SomeText.txt") is StorageFile someTextInLocalStateFolderFile)
{
Logs.Add($"SomeText.txt file exists: {someTextInLocalStateFolderFile.Path}");
SomeTextInLocalStateFolderFile = someTextInLocalStateFolderFile;
}
else
{
Logs.Add($"SomeText.txt file not found in {localStateFolder.Path}");
SomeTextInLocalStateFolderFile = await someTextFile.CopyAsync(localStateFolder);
Logs.Add($"SomeText.txt file created: {SomeTextInLocalStateFolderFile.Path}");
}
}
private async void UpdateSomeTextButton_Click(object sender, RoutedEventArgs e)
{
if (SomeTextInLocalStateFolderFile is null)
{
Logs.Add("Not initialized.");
return;
}
string someTextInLocalStateFolder = await FileIO.ReadTextAsync(SomeTextInLocalStateFolderFile);
someTextInLocalStateFolder += Environment.NewLine + DateTime.Now;
Logs.Add($"SomeText.txt in LocalStateFolder updated to: {someTextInLocalStateFolder}");
await FileIO.WriteTextAsync(SomeTextInLocalStateFolderFile, someTextInLocalStateFolder);
}
}