C# 写配置文件

来源:互联网 发布:java 16进制中文 x 编辑:程序博客网 时间:2024/05/14 09:06

 

 public void saveConfig(string connString)
        {

            System.Xml.XmlDocument doc = new XmlDocument();

            //获得配置文件的全路径

            string filePath = AppDomain.CurrentDomain.BaseDirectory.ToString() + "PowerService.exe.config";

            doc.Load(filePath);

            System.Xml.XmlNodeList nodes = doc.GetElementsByTagName("add");

            for (int i = 0; i < nodes.Count; i++)
            {

                //获得将当前元素的key属性

                System.Xml.XmlAttribute att = nodes[i].Attributes["name"];

                //根据元素的第一个属性来判断当前的元素是不是目标元素

                if (att.Value == "SqlSystem_ConnString")
                {

                    //对目标元素中的第二个属性赋值

                    att = nodes[i].Attributes["connectionString"];

                    att.Value = connString;

                    break;

                }
            }

            doc.Save(filePath);

        }

原创粉丝点击