c# - FileNotFoundException 从 Windows 应用商店应用程序的 Assets 文件夹中读取 JSON 文件

标签 c# windows file-io windows-runtime windows-store

我正在尝试从我的 Assets 文件夹中读取一个 json 文件。我试过很多代码示例,但都是同一件事的变体。

我觉得我一定在做一些愚蠢的事情,但我就是看不到。

string filepath = @"Assets\resources.json";
StorageFolder folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFile file = await folder.GetFileAsync(filepath); // error here
var data = await Windows.Storage.FileIO.ReadTextAsync(file);

每次运行上述代码时,我的应用程序都会收到 System.IO.FileNotFoundException。我已经两次和三次检查文件是否在 Assets 文件夹中,并且我的路径中没有拼写错误。

这个我也试过

StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/resources.json"));

同样的错误。 System.IO.FileNotFoundException

我的 Assets 文件夹的屏幕截图:

Screenshot of my Assets folder

最佳答案

构建操作下的文件属性(右键单击和属性)中,您是否确定将其选为内容?还要确保将复制到输出目录 设置为始终复制。我认为这应该可以解决您的问题。

关于c# - FileNotFoundException 从 Windows 应用商店应用程序的 Assets 文件夹中读取 JSON 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20993849/

相关文章:

c# - Button 上的 WPF Dropshadow 导致文本模糊

java - 在java中获取环境变量值

c# - 检测用户事件

c# - 通用 Windows 平台和 SignalR(无法加载文件或程序集“System.Net,版本 = 2.0.5.0”)

objective-c - Objective C 向文件添加内容

java - 启动在 RAM 内存中创建的文件

c# - 在图像控件中显示位图

c# - Page.IsValid 总是返回 false

c# - 值类型有 Type 对象吗?

javascript - 如何从 html 文件输入类型中删除单个文件?