2012-02-02 175 views
3

更新:我有一個問題,但實際上我的問題可以通過詢問一個稍微不同的問題來解決。爲什麼在某些機器上我的應用程序會拋出錯誤:配置文件後在配置文件中缺少<configSections>

Configuration system failed to initialize - System.Configuration - at  System.Configuration.ConfigurationManager.PrepareConfigSystem() 

其中,在其他機器上它沒有。這裏描述的錯誤.NET 3.5 - Configuration system failed to initialize exception也是由我的app.config頂部缺少configSections元素引起的。當然,這個問題可以通過放入這個部分來解決,但是由於某些原因,我的項目解決方案中的app.config並不是在部署完成後在appdata文件夾中創建的。

原題:

爲什麼我的用戶配置文件是在某些機器上,而不是其他部署時缺少這一部分?我如何確保它不會丟失。

<configSections> 
    <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" > 
     <section name="NameOfAddin_Add_in.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" /> 
    </sectionGroup> 
</configSections> 

一些背景。我通過點擊一次Visual Studio安裝程序在運行Outlook 2007和2010的win 7計算機上部署vsto加載項。

加載項讀取和寫入一些設置到app.config文件,與exe不同存儲在本地用戶的appdata文件夾中。

在某些機器上,但是我發現了一個錯誤「配置系統初始化失敗 - System.Configuration - 在System.Configuration.ConfigurationManager.PrepareConfigSystem()」,這在我的情況下,通過上述缺失的元素而引起 xml。但是在其他機器上,configSections不會丟失。該問題與正在使用的Outlook版本無關。

+0

我今天也遇到了這個問題。你有沒有找到解決方案? – BLSully 2012-12-04 15:14:06

+0

@BLSully:我在下面發佈了一個解決方案... – jreichert 2013-03-18 16:31:46

回答

1

昨天我在VSTO DLL項目中遇到了同樣的問題,我仍然不明白爲什麼with name =「userSettings」有時會丟失。 但我可以提供我的解決方案:我做了一個功能,將來自固定的「.dll.config」文件,在漫遊目錄中的配置文件拷貝丟失的XML部分(如果缺少):

/// <summary> 
    /// Corrects the roaming settings file if needed because sometimes the node "configSections" is missing in the settings file. 
    /// Correct this by taking this node out of the default config file. 
    /// </summary> 
    private static void CorrectRoamingSettingsFileIfNeeded() 
    { 
     const string NODE_NAME_CONFIGURATION = "configuration"; 
     const string NODE_NAME_CONFIGSECTIONS = "configSections"; 
     const string NODE_NAME_USERSETTINGS = "userSettings"; 
     const string ADDIN_DLL_FILENAME = "MyAddIn.dll"; 

     //Exit if no romaing config (file) to correct... 
     var configRoaming = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming); 
     if (configRoaming == null) return; 
     if (!configRoaming.HasFile) return; 

     //Check for the <sectionGroup> with name="userSettings" 
     //Note: Used ugly iteration because "configRoaming.GetSectionGroup(sectionGroupName)" throws ArgumentException. 
     ConfigurationSectionGroup sectionGroupUserSettings = null; 
     foreach (ConfigurationSectionGroup sectionGroup in configRoaming.SectionGroups) 
     { 
      if (sectionGroup.Name.Equals(NODE_NAME_USERSETTINGS)) 
      { 
       sectionGroupUserSettings = sectionGroup; 
       break; 
      } 
     } 

     //Exit if the needed section group is found... 
     if (sectionGroupUserSettings != null && sectionGroupUserSettings.IsDeclared) return; 

     //Do correction actions... 
     var xDoc = XDocument.Load(configRoaming.FilePath); 
     var userSettingsNode = xDoc.Element(NODE_NAME_CONFIGURATION).Element(NODE_NAME_USERSETTINGS); 

     var addInDllConfigFullFilename = AppDomain.CurrentDomain.BaseDirectory + ADDIN_DLL_FILENAME; 
     var configDefault = ConfigurationManager.OpenExeConfiguration(addInDllConfigFullFilename); 
     var xDocDefault = XDocument.Load(configDefault.FilePath); 
     var configSectionsNode = xDocDefault.Element(NODE_NAME_CONFIGURATION).Element(NODE_NAME_CONFIGSECTIONS); 

     userSettingsNode.AddBeforeSelf(configSectionsNode); 
     xDoc.Save(configRoaming.FilePath); 
    }