【问题标题】:ConfigurationManager Class配置管理器类
【发布时间】:2010-07-17 14:46:09
【问题描述】:

如何使用 CConfigurationManager 类读取和写入设置文件?

【问题讨论】:

  • 你来这里之前用谷歌搜索过吗?
  • 是的,我当然有。我有几个链接,我试过了。我还参考了 Andrew Troelson 的一本书,名为“Pro C# 2008 and the .NET 3.5 Platform”。我正在尝试那本书中的例子。书上说,app.exe.config 每次都会被覆盖,我编译。我在 VS 2010 中没有遇到这种情况。

标签: c# .net frameworks configurationmanager


【解决方案1】:

我可以尝试在这里为您提供细分,但实际上以下链接均由 Jon Rista 编写,是我找到的有关 System.Configuration 命名空间的最佳资源。

Unravelling the Mysteries of .NET 2.0 Configuration

Decoding the Mysteries of .NET 2.0 Configuration

Cracking the Mysteries of .NET 2.0 Configuration

【讨论】:

  • 感谢您的链接。最后一个链接包含对 machine.config 文件的引用。该文件包含我在下面对 SpaceCracker 的评论中提到的“流氓”字符串。我相信我已经找到了 .NET 可执行文件的配置管理系统。
【解决方案2】:

可能会引起您注意的一件事是,要访问 ConfigurationManager 类,您必须手动添加对 System.Configuration 的引用。 System.Configuration 命名空间被拆分为多个程序集 - 默认情况下您只能获得其中的一部分。

Space Cracker 发布了一些不错的链接,但也有一种编写强类型配置部分的方法,这是我几年前在博客上写的 - Configuring a .NET 2.0 Application Using the ConfigurationSection class。我确实从未见过其他人使用强类型的 ConfigurationSection,但是一旦您习惯了它们,它们就会非常有用。

【讨论】:

    【解决方案3】:

    【讨论】:

    • 我遇到的一件事是,当我通过 connectionstrings 部分循环时出现了一个流氓字符串,我不知道这个流氓字符串是如何填充的。例如,我的 connectionStrings 部分中有 a> prod b> dev c> uat 字符串。当我循环通过 connectionStrings 部分时,使用此代码。 foreach(ConfigurationManager.ConnectionStrings 中的 ConnectionStringSettings 键){ Console.WriteLine("ConKey: {0}, Value:{1}", key.Name, key.ConnectionString.ToString()); } 我得到 4 个字符串。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-08
    • 2015-10-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-29
    • 1970-01-01
    • 2012-02-02
    相关资源
    最近更新 更多