如何跟踪ConfigurationManager.AppSettings的来源?

时间:2015-07-15 14:18:12

标签: c# configuration

我的AppModel基类上有以下代码:

        public string GetParameter(string key)
        {
            //Allow local config file to override DB setting
            string retval = ConfigurationManager.AppSettings[key];

            if (!string.IsNullOrEmpty(retval))
            {
                Trace.TraceInformation("{0} from ConfigurationManager.AppSettings is '{1}'", key, retval);
                return retval;
            }

            //No setting in config file - check for ProfileParameter from DB
            Parameters.TryGetValue(key, out retval);
            Trace.TraceInformation("{0} from ProfileParameter is '{1}'", key, retval);
            return retval;          
        }

我在Excel Addin中看到一个参数值来自ConfigurationManager.AppSettings,但我不知道它在哪里找到参数 - ConfigurationManager正在使用的配置文件的路径是什么?有没有办法向ConfigurationManager询问其知识来源?

1 个答案:

答案 0 :(得分:0)

在VS(设置>添加新项目)中创建配置对象值时,会为其分配一个默认值(等于创建时的值)。
如果未找到配置文件/未加载/ etc,则使用默认值。

查看项目的 Settings.Designer.cs 文件,了解设置上方的属性global::System.Configuration.DefaultSettingValueAttribute

测试项目中的设置示例:

[global::System.Configuration.ApplicationScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("This is my default string value")] // << This is what you are looking for
public string TestSetting
{
  get
  {
    return ((string)(this["TestSetting"]));
  }
}

[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("False")]  // << This is what you are looking for
public bool AnotherSetting
{
  get
  {
    return ((bool)(this["AnotherSetting"]));
  }
  set
  {
    this["AnotherSetting"] = value;
  }
}
相关问题