FileNotFoundException从Windows Store应用程序中的Assets文件夹读取JSON文件

我试图从我的资产文件夹读取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 。 我已经检查了两倍和三倍的文件是在资产文件夹,并没有在我的path中的拼写错误。

我也试过这个

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

同样的错误。 System.IO.FileNotFoundException

我的资产文件夹的屏幕截图:

我的资产文件夹的屏幕截图

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