C specific methods for manipulating config files

  • 2020-05-17 06:16:30
  • OfStack

The following is the definition of app.config or web.config with one parameter, Isinit as the key and false as the value
< ?xml version="1.0"? >
< configuration >
< appSettings >
< add key ="IsInit" value="false"/ >
< /appSettings >
< /configuration >

Here is the method definition for reading and writing config files:

Writing:


internal void settingApp_write(string key, string val) 
 { 
     System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
 config.AppSettings.Settings["IsInit"].Value = val; 
 config.Save(ConfigurationSaveMode.Modified); 
 ConfigurationManager.RefreshSection("appSettings"); 
 }

Read:

internal string settingApp_read(string key) 
{ 
    var val = ConfigurationManager.AppSettings[key]; 
    return val; 
}

Usage:

Write test:

settingApp_write("IsInit","true");

Take out test:

var setting = settingApp_read("Isinit");

This level of modification is the project level config file modification, which is the operation of the config file in the Bin directory of your final program.


Related articles: