Home  >  Article  >  Backend Development  >  Specific methods of operating config files in C#

Specific methods of operating config files in C#

高洛峰
高洛峰Original
2016-12-24 13:04:561442browse

The following is the definition of app.config or web.config, which defines a parameter with the key Isinit and the value false
dc55d8f1b8d0d15e8e0c4fca7640a4b5
f9d9f4a8f32d97f3ef0c10742ed31240
47063a1a6fd14bf4912f555b4d809c85
< ;add key ="IsInit" value="false"/>
6b501f00052820aa0a488ad052ac34ce
4b1b9d85fe86862ae3eab7e2045cf8a0

The following are the method definitions for reading and writing config files:

Write:

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");

The modifications at this level are Project-level config file modification is the operation of the config file in the Bin directory of your final program.


For more related articles on specific methods of operating config files in C#, please pay attention to the PHP Chinese website!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn