0

我在 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;
}
4

0 回答 0