如何编辑链接的app.config并保存所有exe配置中的更改 [英] How to edit linked app.config and save changes in all exe config

查看:88
本文介绍了如何编辑链接的app.config并保存所有exe配置中的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

提前致谢。



我尝试过:



OpenMappedExeConfiguration和OpenExeConfiguration,但没有任何工作。

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.

推荐答案

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



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


这篇关于如何编辑链接的app.config并保存所有exe配置中的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆