动态添加Web配置中的值?如何+最佳方式

Sin*_*ton 4 c# asp.net web-config

我只想在asp.net中的web.Config中动态地阅读,编写和添加值.记住了很多想法,但我知道什么是动态添加Web配置中值的最佳方法.

例如,在我的情况下,我必须添加

 <identity  userName="someDomain\User" password="password" impersonate="true" />
Run Code Online (Sandbox Code Playgroud)

从后面的代码中标记web配置.

等待好的回应

Mar*_*wan 5

我找到了你想要的代码是:

 public void saveIdentity(string username, string password, bool impersonate)
    {
        Configuration objConfig = WebConfigurationManager.OpenWebConfiguration("~");
        IdentitySection identitySection = (IdentitySection)objConfig.GetSection("system.web/identity");
        if (identitySection != null)
        {
            identitySection.UserName = username;
            identitySection.Password = password;
            identitySection.Impersonate = impersonate;
        }
    objConfig.Save();
}
Run Code Online (Sandbox Code Playgroud)