VS2005 如何读写 app.config
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Collections;
namespace ServicesConfiguration
{
public class ConfigurationDispose
{
public ConfigurationDispose()
{
}
#region GetConfiguration
/// <summary>
/// 取得appSettings里的值
/// </summary>
/// <param name="key">键</param>
/// <returns>值</returns>
public static string GetConfiguration(string key)
{
return ConfigurationManager.AppSettings[key];
}
#endregion
#region GetConfigurationList
/// <summary>
/// 取得appSettings里的值列表
/// </summary>
/// <param name="filePath">配置文件路径</param>
/// <returns>值列表</returns>
public static KeyValueConfigurationCollection GetConfigurationList(string filePath)
{
AppSettingsSection appSection = null; //AppSection对象
Configuration configuration = null; //Configuration对象
KeyValueConfigurationCollection k = null; //返回的键值对类型
configuration = ConfigurationManager.OpenExeConfiguration(filePath);
//取得AppSettings节
appSection = (AppSettingsSection)configuration.Sections["appSettings"];
//取得AppSetting节的键值对
k = appSection.Settings;
return k;
}
#endregion
#region SetConfiguration
/// <summary>
/// 设置appSetting的值
/// </summary>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <param name="filePath">App.config文件路径</param>
public static void SetConfiguration(string key, string value,string filePath)
{
Configuration configuration = null; //Configuration对象
AppSettingsSection appSection = null; //AppSection对象
configuration = ConfigurationManager.OpenExeConfiguration(filePath);
//取得AppSetting节
appSection = configuration.AppSettings;
//赋值并保存
appSection.Settings[key].Value = value;
configuration.Save();
}
#endregion
#region SetConfiguration
/// <summary>
/// 设置appSetting的值
/// </summary>
/// <param name="key">键</param>
/// <param name="value">值</param>
public static void SetConfiguration(string key, string value)
{
AppSettingsSection appSection = null; //AppSection对象
appSection = ConfigurationManager.GetSection("appSettings");
appSection.Settings[key].Value = value;
}
#endregion
}
}
另外一篇是用 XDOC的方法。
关于APP.CONFIG
在做Winform开发时,免不了把一些配置信息写到APP.CONFIG文件中,当程序生成后APP.CONFIG会变成以程序名+CONFIG的文件
即,如程序名为A,那么生成后的APP.CONFIG文件会变成A.EXE.CONFIG文件!
正文
以偶在做项目为例.程序要根据用户设定的时间,自动检查OUTLOOK的"垃圾邮件箱"垃圾邮件.!偶把设定后的时间保存在了APP.CONFIG中
用户设定后再重新读取值显示到界面上,用的方法:ConfigurationSettings.AppSettings,结果发现没有读到.后来才明白,原来CONFIG文件只在程序初次运行才读取值.如果在程序运行中,改变其值然后读取,其实读的是缓存的文件.并不是CONFIG文件本身.如果想动态读取就要用XML的读方法
/** <summary>
/// Read confing
/// </summary>
/// <param name="path"></param>
/// <param name="appKey"></param>
/// <returns></returns>
public string GetConfigValue(string path,string appKey)
{
XmlDocument xDoc = new XmlDocument();
try
{
xDoc.Load(path);
//xDoc.Load(System.Windows.Forms.Application.ExecutablePath+".config");
XmlNode xNode;
XmlElement xElem;
xNode = xDoc.SelectSingleNode("//appSettings");
xElem = (XmlElement)xNode.SelectSingleNode("//add[@key='"+appKey+"']");
if(xElem!=null)
return xElem.GetAttribute("value");
else
return "";
}
catch(Exception)
{
return "";
}
}
我现在的程序中,用的是user Setting的方式,但是 Save后,不会改变 app.exe.config文件,不知道写到哪儿去了,但是程序下次启动时,是有效的,工程太紧,有时间再看看这一块的内容。
还没时间试,弄好了我再贴出来吧。