c#配置文件无法写入?始终还是1没变
Configurationcfa=ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.Non...
Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
cfa.AppSettings.Settings["type1"].Value = "123";
cfa.Save();
System.Configuration.ConfigurationManager.RefreshSection("appSettings");
===================================================================
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="type1" value="1"/>
</appSettings>
</configuration> 展开
cfa.AppSettings.Settings["type1"].Value = "123";
cfa.Save();
System.Configuration.ConfigurationManager.RefreshSection("appSettings");
===================================================================
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="type1" value="1"/>
</appSettings>
</configuration> 展开
1个回答
推荐于2016-02-27
展开全部
app.config 只会在程序启动的时候加载一次。要动态更新可以这样做:
string appPath = System.IO.Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().Location);
string configFile = System.IO.Path.Combine(appPath, "App.config");
ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = configFile;
System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
config.AppSettings.Settings["YourThing"].Value = "New Value";
config.Save();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询