C#读写配置文件
2013-09-06 23:09
369 查看
//可以读写指定的配置文件和本程序的配置文件
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
namespace Config
{
/// <summary>
/// 对exe.Config文件中的appSettings段进行读写配置操作
/// 注意:调试时,写操作将写在vhost.exe.config文件中
/// </summary>
public class ConfigAppSettings
{
private System.Configuration.Configuration config = null;
public ConfigAppSettings(string filename)
{
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = filename;
config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
}
public ConfigAppSettings()
{
config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
}
/// <summary>
/// 写入值
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void SetValue(string key, string value)
{
//增加的内容写在appSettings段下 <add key="RegCode" value="0"/>
//System.Configuration.Configuration config = 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>
/// <param name="key"></param>
/// <returns></returns>
public string GetValue(string key)
{
//System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (config.AppSettings.Settings[key] == null)
return "";
else
return config.AppSettings.Settings[key].Value;
}
}
}
相关文章推荐
- C#读写App.config配置文件
- C#读写INI配置文件
- C#读写配置文件(XML文件)
- C# 读写App.config配置文件的方法
- C# 读写配置文件
- c# WinForm 创建与读写配置文件
- c# 读写自定义配置文件
- C#读写config配置文件的方法
- 在C#中读写INI配置文件(转)
- C#读写ini配置文件
- 用C#读写ini配置文件(转自:http://www.cnblogs.com/zzyyll2/archive/2007/11/06/950584.html)
- C# 读写INI配置文件
- 用C#读写ini配置文件
- 用C#读写ini配置文件
- 【转载】C#.NET 读写 INI配置文件
- c#读写配置文件
- [WPF]C#读写ini配置文件【个人学习用】
- c#读写ini配置文件示例
- C#读写配置文件(XML文件)
- 如何用c#读写配置文件,最后一句帮了我大忙!