2012-02-02 79 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); 
    }