Skip to main content
 首页 » 编程设计

c#之在项目中找不到 Json 文件

2024年02月06日15jirigala

我正在尝试从我的项目中读取一个 json 文件,但 vs 无法找到它。当我使用 .txt 扩展名时,一切正常,但是当我将扩展名更改为 .json 时,找不到文件

string fileContent; 
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(@"ms-appx:///Resources/Database/text.json")); 
using (StreamReader sRead = new StreamReader(await file.OpenStreamForReadAsync())) 
fileContent = await sRead.ReadToEndAsync(); 

"The system cannot find the file specified. (Exception from HRESULT: 0x80070002)"

请您参考如下方法:

为了使用 JSON 文件,您需要在 Visual Studio 的文件资源管理器中右键单击该文件并选择“属性”,然后在“高级”部分:

1) 将Build Action设置为Content

2) 将Copy to Output Directory设置为Copy always