Modificare app.config

CodeKeep C# Feed Novembre 17th, 2008

Description: Modificare app.config

Link: http://www.codekeep.net/snippets/a05eb0b7-af31-4351-82b5-35e286ac64d4.aspx

            string configFileName = Application.StartupPath + @"\app.config";
            XmlDocument configFile = new XmlDocument();
            configFile.Load(configFileName);
            XmlNode configurations = configFile.SelectSingleNode("descendant::configuration");
            XmlNode appConfig = configFile.SelectSingleNode("descendant::appSettings");
            if (appConfig == null)
            {
                appConfig = configFile.CreateElement("appSettings");
                configurations.AppendChild(appConfig);
            }
            // il nodo deve avere la forma: <add key= "Nome del DB"
            // value="Stringa di connessione" />
            XmlNode node = configFile.CreateElement("add");
            XmlAttribute key = configFile.CreateAttribute("key");
            key.Value = "DBUTENTI";
            // aggiungo l'attributo al nodo
            node.Attributes.SetNamedItem(key);
            XmlAttribute val = configFile.CreateAttribute("value");
            val.Value = "Data Source=SRVSQLCAAF;Initial Catalog=ANAUTENTI;Integrated Security=True";
            node.Attributes.SetNamedItem(val);
            // aggiungo il nodo all'elemento superiore
            appConfig.AppendChild(node);
            node = configFile.CreateElement("add");
            key = configFile.CreateAttribute("key");
            key.Value = "DBCOMUNI";
            node.Attributes.SetNamedItem(key);
            val = configFile.CreateAttribute("value");
            val.Value = "Data Source=SRVSQLCAAF;Initial Catalog=COMUNI;Integrated Security=True";
            node.Attributes.SetNamedItem(val);
            appConfig.AppendChild(node);
            configFile.Save(configFileName);

  • .NET
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Comments are closed.

Trackback URI |