我有一个 WINUI 3 应用程序,我在其中添加了多个我自己的文本文件/照片。但是,当我打包应用程序并通过包下载 exe 时,它会尝试从
C:\Program Files\WindowsApps\PackageName\FilePath
加载这些文件。这理所当然地给了它访问被拒绝的错误。
代码:
public static string ProjBaseDirectory = System.AppDomain.CurrentDomain.BaseDirectory;
public static string CountriesListPath = Path.Combine(ProjBaseDirectory, @"Assets\Contents\Docs\Countries.txt");
以上是我尝试访问大部分文件的方式,调试时我从中得到的路径是
C:\user\Projects\iAssist\iAssist\bin\x64\Debug\net6.0-windows10.0.19041.0\win10-x64\AppX\Assets\Contents\Docs\Countries.txt
但是同一条线在打包时给了我
C:\Program Files\WindowsApps\PackageName\FilePath
的路径。
我一直在尝试多种解决方案,比如为文件的属性尝试不同的构建操作选项,但似乎没有任何效果。有没有解决这个问题的方法,我们将不胜感激,我已经在这个问题上停留了两天了。
谢谢!
让我给你看一个基于@Simon Mourier关于
Windows.Storage.ApplicationData
评论的例子。
在这个例子中,我在项目的Assets文件夹中添加了一个文本文件SomeText.txt
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);
}
}