A function of modifying the appSettings configuration section in web.config

xiaoxiao2021-03-06  81

This function mainly uses XMLDocument to parse Web.config. And use the selectsinglenode () method to locate the configuration section you want to modify. It is important to pay attention to the last program to save (), so your APSNET account must have write permissions to Web.config.

---------------------------------------------------------------------------------------------------------------------------------------

// / Modify the value attribute in the addset in the web.config file appSettings configuration section

///

///

/// Note, after calling the function, the entire web application will be restarted, resulting in all current sessions lost

///

/// Key to modify

/// Modified Value

/// Can't find the related key

/// The permissions are not enough, and it cannot be saved in the web.config file

Public void modify (String Key, String Strigue)

{

String xpath = "/ configuration / appsettings / add [@ key = '?']";

XMLDocument Domwebconfig = New XmLDocument ();

DomWebconfig.Load (httpContext.current.server.mappath ("Web.config"));

XMLNode AddKey = DomWebconfig.selectsinglenode ((xpath.replace ("?", Key)));

IF (addKey == NULL)

{

Throw new ArgumentException ("No configuration section");

}

Addkey.attributes ["value"]. innerText = STRVALUE;

Domwebconfig.save (httpContext.current.server.mappath ("Web.config"))))

}

转载请注明原文地址:https://www.9cbs.com/read-121934.html

New Post(0)