• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
yunhuasheng's blog
everything that we can't do now ,but future with our endeavor. springfield!
博客园    首页    新随笔    联系   管理    订阅  订阅

读取和修改App.config配置文件

读取和修改App.config配置文件
 在WinForm中,可以通过添加配置文件(如App.config)来为程序简易地设置一些参数,但为了自己指定配置文件的位置,就不用VS添加了,写两个方法读取和修改指定文件。

   

public class AppSettings
    {
        public static string AppConfig()
        {
            return System.IO.Path.Combine(Application.StartupPath, "App.config");//此处配置文件在程序目录下
        }

        public static string GetValue(string appKey)
        {
            XmlDocument xDoc = new XmlDocument();
            try
            {
                xDoc.Load(AppSettings.AppConfig());
                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 "";
            }
        }

        public static void SetValue(string AppKey, string AppValue)
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(AppSettings.AppConfig());
            XmlNode xNode;
            XmlElement xElem1;
            XmlElement xElem2;
            xNode = xDoc.SelectSingleNode("//appSettings");
            xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
            if (xElem1 != null)
            {
                xElem1.SetAttribute("value", AppValue);
            }
            else
            {
                xElem2 = xDoc.CreateElement("add");
                xElem2.SetAttribute("key", AppKey);
                xElem2.SetAttribute("value", AppValue);
                xNode.AppendChild(xElem2);
            }
            xDoc.Save(AppSettings.AppConfig());
        }
    }

       /// <summary>
       ///
       /// </summary>
       /// <returns></returns>
       public static XmlNode GetAddressValues()
       {
           XmlDocument xDoc = new XmlDocument();
           xDoc.Load(AppConfig());
           XmlNode xNode;
           xNode = xDoc.SelectSingleNode("//MainForm-comboBoxWeb");
           return xNode;
       }

       public static void SetAddressValue(string AppValue)
       {
           XmlDocument xDoc = new XmlDocument();
           xDoc.Load(AppConfig());
           XmlNode element;
           element = xDoc.SelectSingleNode("//MainForm-comboBoxWeb");
            XmlNode node = xDoc.CreateNode(XmlNodeType.Element, "Item", "");
      node.InnerText = AppValue;
      element.AppendChild(node);
           xDoc.Save(AppConfig());
       }

posted @ 2008-03-19 13:38  yunhuasheng  阅读(3330)  评论(3)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3