【发布时间】:2016-07-19 06:22:05
【问题描述】:
我正在开发 winform c# 应用程序,在所有项目中都链接了单个 app.config。 我正在尝试创建一个项目,它可以在 app.config 中编辑和更新连接字符串。但目前我只能更改特定项目的 exe.config。 如何更改特定解决方案中的所有 exe.config? 提前致谢。
【问题讨论】:
标签: c# winforms app-config
我正在开发 winform c# 应用程序,在所有项目中都链接了单个 app.config。 我正在尝试创建一个项目,它可以在 app.config 中编辑和更新连接字符串。但目前我只能更改特定项目的 exe.config。 如何更改特定解决方案中的所有 exe.config? 提前致谢。
【问题讨论】:
标签: c# winforms app-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);
}
【讨论】: