如何编辑链接 app.config 并保存所有 exe 配置中的更改

How to edit linked app.config and save changes in all exe config

我正在开发 winform c# 应用程序,在所有项目中都有一个 app.config 链接。 我正在尝试创建一个项目,它可以在 app.config 中编辑和更新连接字符串。但目前我只能更改特定项目的 exe.config。 如何更改特定解决方案中的所有 exe.config? 提前致谢。

遍历应用程序文件夹中的所有 exe.config 文件并使用 XmlDocument;我正在为所有人更改连接字符串并再次保存它。

  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);
            }