ConfigurationManager.AppSettings 在 .NET Core 2.0 中可用吗?

编程入门 行业动态 更新时间:2024-10-25 20:21:13
本文介绍了ConfigurationManager.AppSettings 在 .NET Core 2.0 中可用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一种方法可以从我的配置文件中读取设置,如下所示:

I've got a method that reads settings from my config file like this:

var value = ConfigurationManager.AppSettings[key];

仅在面向 .NET Standard 2.0 时编译良好.

It compiles fine when targeting .NET Standard 2.0 only.

现在我需要多个目标,所以我更新了我的项目文件:

Now I need multiple targets, so I updated my project file with:

<TargetFrameworks>netcoreapp2.0;net461;netstandard2.0</TargetFrameworks>

但是现在,netcoreapp2.0 的编译失败并显示以下错误消息:

But now, the compilation fails for netcoreapp2.0 with the following error message:

错误 CS0103 当前上下文 (netcoreapp2.0) 中不存在名称ConfigurationManager"

Error CS0103 The name 'ConfigurationManager' does not exist in the current context (netcoreapp2.0)

另外,我创建了一个新的 .NET Core 2.0 控制台应用程序(这次只针对 .NET Core 2.0),但同样在命名空间 System.Configuration 下似乎没有 ConfigurationManager.

Separately, I created a new .NET Core 2.0 console application (only targeting .NET Core 2.0 this time), but likewise there seems to be no ConfigurationManager under the namespace System.Configuration.

我很困惑,因为它在 .NET Standard 2.0 下可用,所以我希望它在 .NET Core 2.0 中可用,因为 .NET Core 2.0 符合 .NET Standard 2.0.

I'm quite confused because it's available under .NET Standard 2.0, so I would expect it to be available in .NET Core 2.0, as .NET Core 2.0 is .NET Standard 2.0 compliant.

我错过了什么?

推荐答案

是的,ConfigurationManager.AppSettings 在引用 NuGet 包后在 .NET Core 2.0 中可用 System.Configuration.ConfigurationManager代码>.

Yes, ConfigurationManager.AppSettings is available in .NET Core 2.0 after referencing NuGet package System.Configuration.ConfigurationManager.

感谢@JeroenMostert 为我提供了解决方案.

Credits goes to @JeroenMostert for giving me the solution.

更多推荐

ConfigurationManager.AppSettings 在 .NET Core 2.0 中可用吗?

本文发布于:2023-11-08 11:05:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569200.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:AppSettings   ConfigurationManager   NET   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!