在.NET中从app.config或web.config读取设置

955

我正在开发一个 C# 类库,需要能够从 web.config 或者 app.config 文件中读取设置(根据 DLL 是从 ASP.NET Web 应用程序还是 Windows Forms 应用程序引用而定)。

我发现:

ConfigurationSettings.AppSettings.Get("MySetting")

这个代码可以工作,但是微软已经将其标记为过时的。

我看到应该使用以下代码:

ConfigurationManager.AppSettings["MySetting"]

然而,System.Configuration.ConfigurationManager 类似乎在 C# 类库项目中不可用。

最佳方法是什么?


63
我读了4个MSDN的例子和文章,最终到了这里。只需要添加一个参考文献...为什么他们不能直接说呢。好问题!+1 - Piotr Kula
1
如果您也想将设置写回,请查看这里了解如何实现。 - Matt
28个回答

1037

对于像下面这样的示例 app.config 文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="countoffiles" value="7" />
    <add key="logfilelocation" value="abc.txt" />
  </appSettings>
</configuration>

您可以使用以下代码读取上述应用程序设置:

using System.Configuration;

如果您的项目中还没有 System.Configuration 的引用,您可能还需要添加一个。然后您可以按照以下方式访问这些值:


string configvalue1 = ConfigurationManager.AppSettings["countoffiles"];
string configvalue2 = ConfigurationManager.AppSettings["logfilelocation"];

154
我喜欢你的回答胜过被采纳的回答。带有例子的回答总是最能够打动我。 - Brendan Vogt
17
我也遇到了这个问题。你尝试过添加System.Configuration引用了吗?问题是VS会让你误以为你已经有了它;你可以使用智能感知获得System.Configuration命名空间,但它没有ConfigurationManager类。只需添加引用即可解决问题。 - Francine DeGrood Taylor
3
System.Configuration包含ConfigurationManager,你的项目可能缺少对System.Configuration的引用。 - TreK
@NightmareGames 这是因为你没有像答案中所说的那样添加引用。如果你使用 ConfigurationSettings 而不是 ConfigurationManager,那么你就是在使用过时的 API。这就是整个问题的关键所在。 - BrainSlugs83
2
有人能告诉我为什么他们认为System.Configuration默认没有添加吗?在大多数应用程序中,这似乎是一个非常基本的需求。 - Todd Vance
显示剩余7条评论

889
您需要在项目的“references文件夹”中添加对“System.Configuration”的引用。 您应该使用已弃用的ConfigurationSettings而是应该使用ConfigurationManager.

1
非常感谢!回答非常直接明了。我正在构建一个控制台应用程序!这个答案救了我的命! - PatsonLeaner
4
这对于 .NET Core 仍然准确吗? - Triynko
@Triynko,你应该明确指定你考虑的.NET Core版本以确认兼容性,因为在撰写本文时,你正在考虑.NET Core 3.1、.NET 5或6。此外,那些阅读本文的人需要注意,C# 9和VS2019-Program.cs不需要引用System.Configuration(不必要)。 - WiiLF

110

针对.NET Framework 4.5和4.6进行更新;以下内容将不再起作用:

string keyvalue = System.Configuration.ConfigurationManager.AppSettings["keyname"];

现在可以通过Properties访问Setting类:

string keyvalue = Properties.Settings.Default.keyname;
请参阅管理应用程序设置以获取更多信息。

2
2010年起的属性。 - Nick Westgate
2
非常感谢您发布这篇文章。我确定 Properties.Settings.Default.MachName 可以工作,但我无法弄清楚为什么 ConfigurationManager.AppSettings["MachName"] 返回 null。 - J. Chris Compton
2
这结束了我长时间的痛苦。谢谢。框架应该警告你旧的方式已经过时了。 - Neil B
15
无法确认。 ConfigurationManager.AppSettings ["someKey"] 在 .NET 4.5、4.6、4.7.1 中可用。 - Ivanhoe
3
无法在vs2022上工作,这是一个非常过时的答案。 - OKEEngine
显示剩余4条评论

40

右键单击您的类库,然后从菜单中选择“添加引用”选项。

在.NET选项卡中,选择System.Configuration。这将把System.Configuration DLL文件包含到您的项目中。


在添加引用后,可以执行ConfigurationManager.ConnectionStrings[0].ConnectionString - SushiGuy

30

我在使用这个,它对我很有效:

textBox1.Text = ConfigurationManager.AppSettings["Name"];

50
TS明确表示他使用相同的代码,但是他的项目无法编译(后来发现是由于缺少引用)。-1分是因为没有仔细阅读问题。 - Isantipov

29

读取配置文件:

您需要将配置文件添加到引用中:

  1. 打开项目的“属性”
  2. 切换到“设置”选项卡
  3. 添加“名称”和“值”
  4. 使用以下代码获取值:

    string value = Properties.Settings.Default.keyname;
    

保存到配置文件中:

   Properties.Settings.Default.keyName = value;
   Properties.Settings.Default.Save();

1
FYI:Google 最喜欢您的答案。当您搜索“获取应用程序配置设置 c#”时,它会逐字显示。 - Steve Gomez

22

您可能正在将App.config文件添加到DLL文件中。 App.Config仅适用于可执行项目,因为所有DLL文件都从正在执行的EXE文件的配置文件中获取配置。

假设您的解决方案中有两个项目:

  • SomeDll
  • SomeExe

您的问题可能与将app.config文件包含在SomeDLL而不是SomeExe中有关。 SomeDll能够从SomeExe项目中读取配置。


哇,这不是很明显啊。如果有人能够提供相关的文档链接,那就太棒了。这是一个很难搜索到的主题。 - David Krider
谢谢。没有在任何地方看到这个声明。 - parameter

22
您必须将System.Configuration程序集添加到项目的引用中。

14

试一试:

string keyvalue = System.Configuration.ConfigurationManager.AppSettings["keyname"];

web.config文件中,应该使用以下结构:

<configuration>
<appSettings>
<add key="keyname" value="keyvalue" />
</appSettings>
</configuration>

11

步骤1:右键单击“引用”选项卡以添加引用。

步骤2:点击“程序集”选项卡

步骤3:搜索“System.Configuration”

步骤4:单击“确定”。

然后它就会起作用。

 string value = System.Configuration.ConfigurationManager.AppSettings["keyname"];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接