陈宝刚---享受生活,追逐梦想!
理想是心中的火焰,有追求的人才是幸福的人!

配置文件:App.Config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
  <add key="ConnenctionString" value="Data Source=RUBY;Database=Huiyuan;User id=sa;PWD=" />
  <add key="TmpPath" value="C:\Temp" />
 </appSettings>

</configuration>

写XML:

            //App.config配置文件写的方法
            con = "Data Source=" + sql.Text.ToString() + ";Database=" + db.Text.ToString() + ";User id=" + usr.Text.ToString() + ";PWD=" + pwd.Text.ToString() + "";
            SaveConfig("ConnenctionString", con);

        public void SaveConfig(string configKey, string configValue)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(Application.StartupPath +//app.config"); />            

            XmlNodeList nodes = doc.GetElementsByTagName("add");
            foreach (XmlNode node in nodes)
            {
                XmlElement xe = (XmlElement)node;
                if (xe.GetAttribute("key") == configKey)
                {
                    xe.SetAttribute("value", configValue);
                }
            }
            doc.Save(Application.StartupPath + //app.config");
        }

 

读XML:

        public static string GetValue(string appKey)
        {
            try
            {
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(Application.StartupPath + //app.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 "";
            }
        }

posted on 2008-11-23 12:12  追梦人RUBY  阅读(226)  评论(0)    收藏  举报