1.往项目中添加配置文件
添加项目选择“应用程序配置文件”即可生成App.config
新配置文件里面是这样的
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
</configuration>
在configuration标签中添加
<configuration>
<appSettings>
<add key="scrapyInterval" value="5000"/>
</appSettings>
</configuration>
其中 key 和 value 为配置的名称和内容
2.读取配置文件内容
Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
String Interval = cfa.AppSettings.Settings["scrapyInterval"].Value;
3.写入配置文件内容
Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
String input = intervalBox.Text;
cfa.AppSettings.Settings["scrapyInterval"].Value = intervalBox.Text;
cfa.Save();
注意写入配置文件后,Configuration cfa需要重新打开才会获取到新修改的数据
参考
https://jingyan.baidu.com/article/8275fc86b1877a46a13cf674.html
https://www.cnblogs.com/fuchongjundream/p/4206314.html
https://blog.csdn.net/leowangzi/article/details/6679114