1、app.config文件
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="FilePaths" value="E:\Project Code\UpMonitor8.17\UpMonitor\bin\Debug\文件备份;E:\Project Code\UpMonitor8.17\UpMonitor\bin\Debug\Log"/>
<add key="SaveData" value="7"/>
</appSettings>
</configuration>
2、根据Key获取值
string FilePaths = ConfigurationManager.AppSettings["FilePaths"];
注意:需要添加【System.Configuration.dll】引用
3、修改Key的值
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
cfac.AppSettings.Settings["MAC"].Value = string.Empty;
cfac.Save();
Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
cfa.AppSettings.Settings["MAC"].Value = mac;
cfa.Save();
4、参考网址【https://blog.youkuaiyun.com/weixin_30425949/article/details/96756367】
5、读取指定文件下app.config文件
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = @"F:\App1.config"; ;
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
string connstr = config.ConnectionStrings.ConnectionStrings["connStr"].ConnectionString;
MessageBox.Show(connstr);
string key = config.AppSettings.Settings["key"].Value;
MessageBox.Show(key);
本文介绍了如何使用C#读取和修改app.config配置文件中的键值对,并提供了具体的代码实例。包括了基本的读写操作、修改特定键的值以及从指定路径下的配置文件中获取连接字符串等关键内容。
243

被折叠的 条评论
为什么被折叠?



