将windows应用程序配置文件的<appSettings>节中的值映射到控件的属性,
在控件属性更改后,怎么保存到配置文件中啊?
我以前用修改XML文件保存,这样太啰唆了,有没有更好的办法?

解决方案 »

  1.   

    用这个类:
    using System;
    using System.Xml;
    using System.Windows.Forms;namespace Common
    {
    /// <summary>
    /// 模块名称: 读写系统配置类
    /// 编写日期: 2005-12-01
    /// </summary>
    public class AppConfig
    {
    public static bool UpdateConfig(string strKey, string strValue)
    {
    XmlDocument doc = new XmlDocument();
    try
    {
    doc.Load(Application.ExecutablePath + ".config");
    XmlNode node = doc.SelectSingleNode(@"//add[@key='" + strKey + "']");
    XmlElement ele = (XmlElement)node;
    ele.SetAttribute("value", strValue);
    doc.Save(Application.ExecutablePath + ".config");
    }
    catch
    {
    return false;
    }
    return true;
    }
    public AppConfig()
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
    }
    public static string GetConfig(string strKey)
    {
    //return System.Configuration.ConfigurationManager.AppSettings[strKey];
    XmlDocument doc = new XmlDocument();
    try
    {
    doc.Load(Application.ExecutablePath + ".config");
    XmlNode node = doc.SelectSingleNode(@"//add[@key='" + strKey + "']");
    XmlElement ele = (XmlElement)node;
    return ele.GetAttribute("value");
    }
    catch
    {
    return string.Empty;
    }
    }
    }
    }
      

  2.   

    windows有这样的应用程序配置文件吗?
    学习一下...
      

  3.   

    呵呵,VS2005中有,文件名叫App.Config,安装后文件会生成一个名叫:应用程序名.exe.Config的XML文件。
      

  4.   

    哦,你要添加一个啊,在添加对话框中选择应用程序配置文件,使用嘛,跟web.config差不多啊,可以用System.Configuration.ConfigurationManager.AppSettings["..."]来获取。
      

  5.   

    多谢:)
    Snowdust(雪尘) ( ) 信誉:100    Blog 
      

  6.   

    app.config有个问题:
    在运行时修改<appSettings>节点值,然活再用
    System.Configuration.ConfigurationSettings.AppSettings["key"]获取值,
    结果还是未修改前的值
      

  7.   

    那是当然的,要重启应用程序才能取得更改后的值。所以动态改app.config没啥意义,如果要看到即时的修改结果,可以用注册表存配置信息。
      

  8.   

    如果是2003,本质上还是需要通过修改app.config的方式来处理,如果是2005,
    增加了Settings.setting设置文件,可以对用户级别的配置文件进行直接修改。
    http://www.microsoft.com/china/msdn/library/langtool/vcsharp/SettingsCSRL.mspx?mfr=true