从项目中的 Json 文件中获取内容

Get content from Json file inside the project

我正在尝试从位于我的项目中的文件中获取内容。但我一直得到 "Could not find a part of the path "

using (StreamReader r = new StreamReader(Directory.GetCurrentDirectory() + @"/Resources/Database/questionsTEST.json"))

我的 JSON 文件位于 Resources/Database/questionsTEST.json.

我也尝试使用 test.txt 位于主项目中,但仍然没有。

您的应用程序正在从与您当前构建设置相对应的项目的子目录中执行,例如 'YourProject\bin\Debug'。您说该文件是您项目的一部分,因此它位于此处 'YourProject\test.txt',但是您的代码正在查找 'YourProject\bin\Debug\test.txt'.

您需要将文件 test.txt 作为项目输出的一部分。在项目中,右键单击 test.txt 并将输出类型更改为内容并将 'Copy to Output' 更改为 'Copy if Newer'。