在c#lib中手动读取app.settings的问题

时间:2012-09-18 12:42:03

标签: c# configuration app-config

我在c#lib中手动读取应用程序设置时遇到问题。我必须从dll app.config的appsettings部分检索LDAPUser。附上代码示例:

[ConfigurationProperty("LDAPUser")]
private string LDAPUser
{
    get
    {
        Configuration config = null;
        string exeConfigPath = this.GetType().Assembly.Location;
        try
        {
            config = ConfigurationManager.OpenExeConfiguration(exeConfigPath);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
            return string.Empty;
        }
        if (config != null)
        {
            string myValue = GetAppSetting(config, "LDAPUser");
            return myValue;
        }
        return string.Empty;
    }

}

string GetAppSetting(Configuration config, string key)
{
    KeyValueConfigurationElement element = config.AppSettings.Settings[key];
    if (element != null)
    {
        string value = element.Value;
        if (!string.IsNullOrEmpty(value))
            return value;
    }
    return string.Empty;
}

0 个答案:

没有答案