c# - 部署后配置文件中缺少 <configSections>

标签 c# outlook vsto app-config

更新:我在下面有一个问题,但实际上我的问题可以通过问一个稍微不同的问题来解决。为什么我的应用程序在某些机器上会抛出错误:

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>

一些背景。我在运行 outlook 2007 和 2010 的 win 7 机器上通过单击一次 visual studio 安装程序部署 vsto 加载项。

加载项读取和写入一些设置到 app.config 文件,这与 exe 不同,它存储在本地用户的 appdata 文件夹中。

但是在某些机器上我收到错误“配置系统无法初始化 - System.Configuration - 在 System.Configuration.ConfigurationManager.PrepareConfigSystem()” 在我的例子中,这是由上述 xml 中缺少的元素引起的。但是在其他机器上,configSections 并没有丢失。该问题与所使用的 Outlook 版本无关。

最佳答案

我昨天在我的 VSTO DLL 项目中遇到了同样的问题,但我仍然不明白为什么 with name="userSettings"有时会丢失的原因。 但我可以提供我的解决方案:我已经创建了一个函数,将丢失的 XML 部分(如果丢失)从固定的“.dll.config”文件复制到 ROAMING 目录中的配置文件:

    /// <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);
    }

关于c# - 部署后配置文件中缺少 <configSections>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9111504/

相关文章:

c# - 当前使用 C# 扩展 Excel 的最佳方法是什么?

c# - 提高从大型 XML 字符串反序列化的性能

c# - 在哪里捕获异步 WCF 调用的 EndpointNotFoundException?

c# - HTML 中的嵌入图像未出现在 Outlook 渲染器中

html - Outlook 2010 与 Outlook 2013 的电子邮件 html 模板

c# - VSTO c# 检查单元格是否为空

c# - 努力.Exceptions.EffortException : Database has not been initialized - after non commited update?

c# - WebClient 不支持并发 I/O 操作。

java - 用于获取 session 室详细信息的 Outlook API,无论房间是否已预订

c# - 是否可以编写在 Excel 2003 和 2007 中都有效的插件?