0

我向我的 .NetCore 3.1 控制台应用程序添加了一个 config.json 文件。项目。它包含一些键值对、连接字符串等。

现在,当我尝试运行我的应用程序时,我的 Program.cs 文件中引发的异常如下:

未找到配置文件“config.json”且不是可选的。物理路径是......

我修改了我的 .csproj 文件作为一种有希望的补救措施,并添加了以下部分:

<ItemGroup>
      <Content Update="config.json">
        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      </Content>
   </ItemGroup>

以上没有太大帮助。然而得到同样的错误。我从 Program.cs 发布了一部分代码存根:

static void Main(string[] args)
        {
            var jsonPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "config.json");

            var builder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile(jsonPath);

            var configuration = builder.Build(); <-- here exception
            var connectionString = configuration["connectionString"];
            .....
            .....
            .....
            .....

知道它是什么吗?json 文件必须复制到它的调试 bin 文件夹吗?我最近注意到在我的 .Net 应用程序中遇到了一大堆路径读/写问题。使用 Visual Studio,即使路径很好。

请对此提出一些建议/建议。

4

0 回答 0