0

我想要在用戶連接字符串輸入數據的高級安裝程序.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; 
    } 
} 

回答

2

此行爲是由ConfigurationManager.OpenExeConfiguration期待你提供一個可執行文件,而不是一個配置文件的路徑造成的。

要明確地打開一個配置文件,需要使用帶有地圖的過載:

var map = new ExeConfigurationFileMap { ExeConfigFilename = configFilePath }; 
configuration = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None); 
+0

哇它的作品,你是男人。應該很久以前問過了。再次是這裏最好的網站:) – radbyx

+1

不客氣。樂於幫助 :) –

相關問題