保存到web.config,添加一个额外的“配置”扩展
问题描述:
我想要在用户连接字符串输入数据的高级安装程序.MSI向导中创建一个web.config文件。保存到web.config,添加一个额外的“配置”扩展
为什么会产生web.config.config
文件带有额外的config
,我该如何避免它?
我想我的打开或保存不正确?
这是一个自定义操作,在.MSI中,我从Advanced Installer运行,但它应该没有任何影响,我认为。
[CustomAction]
public static ActionResult EncryptConnStr(Session session)
{
try
{
var path = Path.Combine(@"C:\Users\radbyx\Documents", "web.config");
var connStr = BuildConnStr("foo", "foo", "foo", "foo");
Configuration config = ConfigurationManager.OpenExeConfiguration(path);
ConnectionStringsSection section = (ConnectionStringsSection)config.GetSection("connectionStrings");
section.ConnectionStrings.Add(new ConnectionStringSettings(GetConnectionStringName(), connStr));
// Encrypt
//section.SectionInformation.ProtectSection(ConnStrEncryptionKey);
// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified, true);
return ActionResult.Success;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "Trace: " + ex.StackTrace, ex.Message);
throw;
}
}
答
此行为是由ConfigurationManager.OpenExeConfiguration
期待你提供一个可执行文件,而不是一个配置文件的路径造成的。
要明确地打开一个配置文件,需要使用带有地图的过载:
var map = new ExeConfigurationFileMap { ExeConfigFilename = configFilePath };
configuration = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
哇它的作品,你是男人。应该很久以前问过了。再次是这里最好的网站:) – radbyx
不客气。乐于帮助 :) –