读配置很简单,可以用ConfigurationManager.AppSettings[key] 来读出,
可是写配置文件时,如果写成这样
ConfigurationManager.AppSettings[key] = "111";
总是提示只读,那么该怎么办呢?
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Text;
- usingSystem.Configuration;
- namespaceBQKJ.Common
- {
- ///<summary>
- ///对exe.Config文件中的appSettings段进行读写配置操作
- ///注意:调试时,写操作将写在vhost.exe.config文件中
- ///</summary>
- publicclassConfigAppSettings
- {
- ///<summary>
- ///写入值
- ///</summary>
- ///<paramname="key"></param>
- ///<paramname="value"></param>
- publicstaticvoidSetValue(stringkey,stringvalue)
- {
- //增加的内容写在appSettings段下<addkey="RegCode"value="0"/>
- System.Configuration.Configurationconfig=ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- if(config.AppSettings.Settings[key]==null)
- {
- config.AppSettings.Settings.Add(key,value);
- }
- else
- {
- config.AppSettings.Settings[key].Value=value;
- }
- config.Save(ConfigurationSaveMode.Modified);
- ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件
- }
- ///<summary>
- ///读取指定key的值
- ///</summary>
- ///<paramname="key"></param>
- ///<returns></returns>
- publicstaticstringGetValue(stringkey)
- {
- System.Configuration.Configurationconfig=ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- if(config.AppSettings.Settings[key]==null)
- return"";
- else
- returnconfig.AppSettings.Settings[key].Value;
- }
- }
- }
其实也很简单,用这两个封装过的方法就可以了。
需要注意的是,在IDE调试时,写入的配置文件其实是写在了.vshost.exe.config文件中,所以你在.exe.config中是看不到的。只有直接运行exe文件时,才会正确写入到.exe.config中。
上一篇:
SQL日期格式化应用大全
下一篇:
Web服务配置错误: allowDefinition='MachineToApplication' 错误和 客户端发现响应内容类型为“text/html; charset=utf-8”...
读配置很简单,可以用ConfigurationManager.AppSettings[key] 来读出,
可是写配置文件时,如果写成这样
ConfigurationManager.AppSettings[key] = "111";
总是提示只读,那么该怎么办呢?
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Text;
- usingSystem.Configuration;
- namespaceBQKJ.Common
- {
- ///<summary>
- ///对exe.Config文件中的appSettings段进行读写配置操作
- ///注意:调试时,写操作将写在vhost.exe.config文件中
- ///</summary>
- publicclassConfigAppSettings
- {
- ///<summary>
- ///写入值
- ///</summary>
- ///<paramname="key"></param>
- ///<paramname="value"></param>
- publicstaticvoidSetValue(stringkey,stringvalue)
- {
- //增加的内容写在appSettings段下<addkey="RegCode"value="0"/>
- System.Configuration.Configurationconfig=ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- if(config.AppSettings.Settings[key]==null)
- {
- config.AppSettings.Settings.Add(key,value);
- }
- else
- {
- config.AppSettings.Settings[key].Value=value;
- }
- config.Save(ConfigurationSaveMode.Modified);
- ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件
- }
- ///<summary>
- ///读取指定key的值
- ///</summary>
- ///<paramname="key"></param>
- ///<returns></returns>
- publicstaticstringGetValue(stringkey)
- {
- System.Configuration.Configurationconfig=ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- if(config.AppSettings.Settings[key]==null)
- return"";
- else
- returnconfig.AppSettings.Settings[key].Value;
- }
- }
- }
其实也很简单,用这两个封装过的方法就可以了。
需要注意的是,在IDE调试时,写入的配置文件其实是写在了.vshost.exe.config文件中,所以你在.exe.config中是看不到的。只有直接运行exe文件时,才会正确写入到.exe.config中。