C # asp.net modify the webconfig file configuration # region modify the config file // <summary> // modify the config file (AppSetting node) /// </summary> /// <param name = "key"> key </param> /// <param name = "value"> value to be modified </param> public static void UpdateAppSetting (string key, string value) {XmlDocument doc = new XmlDocument (); // obtain the full path of the configuration file string strFileName = AppDomain. currentDomain. baseDirectory. toString () + "Web. config "; doc. load (strFileName); // find All the elements named "add" XmlNodeList nodes = doc. getElementsByTagName ("add"); for (int I = 0; I <nodes. count; I ++) {// obtain the key attribute XmlAttribute _ key = nodes [I] of the current element. attributes ["key"]; // determines whether the current element is the target element if (_ key! = Null) {if (_ key. value = key) {// Value _ key = nodes [I] For the second attribute in the target element. attributes ["value"]; _ key. value = value; break ;}}// Save the modified doc above. save (strFileName) ;}/// <summary> // modify the config file (ConnectionString node) /// </summary> /// <param name = "name"> key </param> /// <param name = "value"> value to be modified </param> public static void UpdateConnectionString (string name, string value) {XmlDocument doc = new XmlDocument (); // Obtain the full path of the configuration file string strFileName = AppDomain. currentDomain. baseDirectory. toString () + "Web. config "; doc. load (strFileName); // find all elements named "add" XmlNodeList nodes = doc. getElementsByTagName ("add"); for (int I = 0; I <nodes. count; I ++) {// obtain the key attribute XmlAttribute _ name = nodes [I] of the current element. attributes ["name"]; // determines whether the current element is the target element if (_ name! = Null) {if (_ name. value = name) {// Value _ name = nodes [I] For the second attribute in the target element. attributes ["connectionString"]; _ name. value = value; break ;}}// Save the modified doc above. save (strFileName) ;}# endregion