【问题标题】:How to use ConfigurationManager如何使用配置管理器
【发布时间】:2013-10-14 11:45:52
【问题描述】:

我想使用 App.config 来存储一些设置。 我尝试使用下一个代码从配置文件中获取参数。

private string GetSettingValue(string paramName)
{
    return String.Format(ConfigurationManager.AppSettings[paramName]);
}

我还为它添加了System.Configuration(我使用了一个单独的类),并且在 App.config 文件中我有:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <appSettings>
    <add key ="key1" value ="Sample" />
  </appSettings>
</configuration>

但我在尝试使用ConfigurationManager - ConfigurationManager can't exist in such context 时出错,但我已经添加了System.Configuration。还是我错过了什么?

编辑

配置类(全视图)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;

namespace browser
{
    class ConfigFile
    {
        private string GetSettingValue(string paramName)
        {
            return String.Format(ConfigurationManager.AppSettings[paramName]);
        }
    }
}

EDIT2

添加外观

这意味着问题不是在使用ConfigurationManger 期间而是在之前 - 程序“说”它“不知道这样的元素”,因为我理解错误 - “元素配置管理器”在这样的上下文中不存在"

EDIT3

编辑 4

【问题讨论】:

  • 您是否在.cs 文件的顶部添加了using System.Configuration
  • 是的 - 我正在写它“但我已经添加了 System.Configuration”
  • 所以这句话并不完全合格。但是您是说您向System.Configuration 添加了引用 以及 using System.Configuration 对吗?
  • string 后面的这个点是错字吗?
  • @Jack 你到底是什么意思?

标签: c# .net winforms configuration config


【解决方案1】:

好的,我花了一段时间才看到这个,但它无法编译:

return String.(ConfigurationManager.AppSettings[paramName]);

您甚至没有调用 String 类型的方法。只需这样做:

return ConfigurationManager.AppSettings[paramName];

AppSettings KeyValuePair 已经返回一个字符串。如果名称不存在,则返回null


根据您的编辑,您尚未为您正在处理的项目System.Configuration 程序集添加参考

【讨论】:

    【解决方案2】:

    转到tools >> nuget >> console 并输入:

    Install-Package System.Configuration.ConfigurationManager 
    

    如果您想要特定版本:

    Install-Package System.Configuration.ConfigurationManager -Version 4.5.0
    

    您的ConfigurationManager dll 现在将被导入,代码将开始工作。

    【讨论】:

      【解决方案3】:

      按照以下步骤添加对 System.Configuration.dll 的引用:

      在“项目”菜单上,选择“添加引用”。
      在“添加引用”对话框中,选择 .NET 选项卡。
      查找并选择 System.Configuration 的组件名称。
      选择确定。

      这应该添加 ConfigurationManager

      Store and retrieve custom information from an application configuration file

      【讨论】:

        【解决方案4】:

        我找到了一些答案,但我不知道这是否是正确的方法。这是我现在的解决方案。幸运的是它没有破坏我的设计模式。

            /// <summary>
            /// set config, if key is not in file, create
            /// </summary>
            /// <param name="key">Nome do parâmetro</param>
            /// <param name="value">Valor do parâmetro</param>
            public static void SetConfig(string key, string value)
            {
                var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                var settings = configFile.AppSettings.Settings;
                if (settings[key] == null)
                {
                    settings.Add(key, value);
                }
                else
                {
                    settings[key].Value = value;
                }
                configFile.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);
            }
        
            /// <summary>
            /// Get key value, if not found, return null
            /// </summary>
            /// <param name="key"></param>
            /// <returns>null if key is not found, else string with value</returns>
            public static string GetConfig(string key)
            {
                return ConfigurationManager.AppSettings[key];
            }
        

        【讨论】:

        • 两个用户留下了负面评价。提供一个说明他们为什么认为代码不够好是有建设性的。我发现如果我的应用程序位于 C:\Program Files (x86)\MyApp 中,代码会导致崩溃,因为应用程序无权在其中保存数据。
        • 最初的用户问题不是关于创建一种方法来保存丢失的配置,而是由于缺少导入而无法检索它。这就是为什么这个问题可能被低估的原因:它具有误导性。
        • 这个方法效果很好,但是有个问题。它将设置保存在 XML 格式的“.config”文件中,并与已编译的可执行文件一起保存。如果您构建安装程序 MSI 并且本地管理员安装该应用程序,则配置文件将在 c:\program files... 文件夹的子文件夹中结束。最终用户无权写入此文件夹,因此在这种情况下尝试修改配置时会出错。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-21
        相关资源
        最近更新 更多