【问题标题】:Edit app.config values dynamically动态编辑 app.config 值
【发布时间】:2014-01-05 03:07:14
【问题描述】:

我想从我的项目中动态更改一个值是我的 app.config。

这是我的 app.config:

<?xml version="1.0"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
 <system.web>
   <membership defaultProvider="SqlProvider">
   <providers>
    <clear />
    <add name="SqlProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="MySqlConnection"
         enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false"
         applicationName="app1" requiresUniqueEmail="false"
         passwordFormat="Hashed" maxInvalidPasswordAttempts="6545"
         minRequiredPasswordLength="4" minRequiredNonalphanumericCharacters="0"
         passwordAttemptWindow="10" passwordStrengthRegularExpression=""/>
   </providers>
   </membership>
 </system.web>
</configuration>

我想更改 'sqlProvider' 中的 'applicationName' 键的值。

我在网上能找到的只是人们使用:

 ConfigurationManager.AppSettings["key"]

但这对我不起作用。

【问题讨论】:

  • 为什么要改变它?你可以改用 applicationName="/" 吗?

标签: c# configuration app-config


【解决方案1】:

ConfigurationManager.AppSettings["key"] 通常与“AppSettings”部分下定义的设置一起使用。你可以试试下面的代码-

public class MembershipSetting
{
    /// <summary>
    /// Gets or sets the name of the setting.
    /// </summary>
    public string SettingName { get; set; }

    /// <summary>
    /// Gets or sets the setting value.
    /// </summary>
    public string SettingValue { get; set; }
}

  private List<MembershipSetting> GetMembershipSetting()
    {
        List<MembershipSetting> settings = new List<MembershipSetting>
                            {
                                new MembershipSetting {SettingName = "Dafult Membership Provider", SettingValue = Membership.Provider.ToString() },
                                new MembershipSetting {SettingName = "Minimum Required Password Length", SettingValue = Membership.MinRequiredPasswordLength.ToString(CultureInfo.InvariantCulture) },
                                new MembershipSetting {SettingName = "Minimum Required Non Alphanumeric Characters",SettingValue = Membership.MinRequiredNonAlphanumericCharacters.ToString(CultureInfo.InvariantCulture)},
                                new MembershipSetting {SettingName = "Password reset enabled", SettingValue = Membership.EnablePasswordReset.ToString()},
                                new MembershipSetting {SettingName = "Maximum Invalid Password Attempts",SettingValue = Membership.MaxInvalidPasswordAttempts.ToString(CultureInfo.InvariantCulture) },
                                new MembershipSetting {SettingName = "Attempt windows",SettingValue = Membership.PasswordAttemptWindow.ToString(CultureInfo.InvariantCulture)},
                                new MembershipSetting {SettingName = "applicationName",SettingValue = Membership.ApplicationName.ToString(CultureInfo.InvariantCulture)}
                            };

        return settings;
    }

本文最初发布于here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-30
    • 1970-01-01
    • 1970-01-01
    • 2010-11-24
    • 1970-01-01
    • 2015-10-02
    • 1970-01-01
    • 2010-09-21
    相关资源
    最近更新 更多