0
我正在使用winform c#應用程序,在所有項目中鏈接單個app.config。 我試圖創建一個項目,它可以編輯和更新app.config中的連接字符串。但目前我只能改變特定項目的exe.config。 如何在特定的解決方案中更改所有的exe.config? 在此先感謝。如何編輯鏈接的app.config並保存所有exe配置中的更改
我正在使用winform c#應用程序,在所有項目中鏈接單個app.config。 我試圖創建一個項目,它可以編輯和更新app.config中的連接字符串。但目前我只能改變特定項目的exe.config。 如何在特定的解決方案中更改所有的exe.config? 在此先感謝。如何編輯鏈接的app.config並保存所有exe配置中的更改
循環遍歷應用程序文件夾中的所有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);
}