Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on winform c# application, having single app.config linked in all the projects. I'm trying to create one project, which can edit and update connection string in app.config. But currently I'm only able to change particular project's exe.config. How I can change all exe.config in particular solution?
Thanks in advance.

What I have tried:

OpenMappedExeConfiguration and OpenExeConfiguration, but nothing is working.
Posted
Updated 24-Jul-16 21:08pm

1 solution

Loop through all exe.config file in application folder and using XmlDocument; I'm changing connection string for all and saving it again.

string curAssembly = Assembly.GetExecutingAssembly().Location;
string FolderPath = Path.GetDirectoryName(curAssembly);
string[] files = Directory.GetFiles(FolderPath).Where(x => x.EndsWith(".config")).ToArray();
                foreach (string item in files)
                {
                    XmlDocument XmlDoc = new XmlDocument();
                    XmlDoc.Load(item);
                    foreach (XmlElement xElement in XmlDoc.DocumentElement)
                    {
                        if (xElement.Name == "connectionStrings")
                        {
                            foreach (XmlElement xChild in xElement)
                            {
                                if (xChild.Attributes.Count > 1 && xChild.Attributes[0].Value == ConfigSectionName)
                                {
                                    xChild.Attributes[1].Value = "Data Source=" + cmbDatasource.Text + ";Initial Catalog=" + cmbDatabaseName.Text + ";UID=" + txtUserName.Text + ";password=" + txtPassword.Text + ";Integrated Security = false;";
                                    Connectionstring = xChild.Attributes[1].Value;
                                }
                            }
                        }
                    }
                    XmlDoc.Save(item);
                }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900