【问题标题】:How to edit linked app.config and save changes in all exe config如何编辑链接的 app.config 并保存所有 exe 配置中的更改
【发布时间】:2016-07-19 06:22:05
【问题描述】:

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

【问题讨论】:

    标签: c# winforms app-config


    【解决方案1】:

    遍历应用程序文件夹中的所有 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);
                }
    

    【讨论】:

      猜你喜欢
      • 2016-08-07
      • 1970-01-01
      • 2015-08-02
      • 2010-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-06
      • 2012-08-28
      相关资源
      最近更新 更多