2012-12-03 45 views
0

我正在修改Web安裝程序自定義操作期間Web.config文件上的連接字符串。ConfigurationManager將意外的項目添加到system.web部分

這是到目前爲止好是做的工作

ExeConfigurationFileMap map = new ExeConfigurationFileMap(); 
map.ExeConfigFilename = path; 

Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, System.Configuration.ConfigurationUserLevel.None); 

var connectionsection = config.ConnectionStrings.ConnectionStrings; 

ConnectionStringSettings connectionstring = connectionsection[connStringName]; 
if (connectionsection != null) 
    connectionsection.Remove(connStringName); 

connectionstring = new ConnectionStringSettings(connStringName, newValue, "System.Data.SqlClient"); 
connectionsection.Add(connectionstring); 

config.Save(ConfigurationSaveMode.Modified, true); 
ConfigurationManager.RefreshSection("connectionStrings"); 

的代碼片段,這是實際工作,但同時也增加了一些項目,以「System.Web程序」一節,這是導致錯誤:

Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately. 

Parser Error Message: It is an error to use a section registered as allowDefinition='MachineOnly' beyond machine.config. 
Source Error: 

Line 46:  <authorization /> 
Line 47:  <clientTarget /> 
Line 48:  <deployment /> 
Line 49:  <deviceFilters /> 
Line 50:  <fullTrustAssemblies /> 

當我手動刪除通過ConfigurationManager中<deployment /><protocols /><processModel />加入了一些章節錯誤消失。所以我只需要ConfigurationManager不要創建這些部分。怎麼做?

感謝

回答

0

速戰速決將是清楚它在XML手動ConfigurationManager中弄髒文件之後,但這並不是最合適的,我覺得

config.Save(ConfigurationSaveMode.Modified, true); 

var document = XDocument.Load(configPath); 
var systemWeb = document.Root.Element("system.web"); 

XElement element; 
element = systemWeb.Element("deployment"); 
if (element != null) 
    element.Remove(); 

element = systemWeb.Element("protocols"); 
if (element != null) 
    element.Remove(); 

element = systemWeb.Element("processModel"); 
if (element != null) 
    element.Remove(); 

document.Save(configPath); 
相關問題