ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C#-部署后缺少配置文件

2019-12-01 21:05:35  阅读:557  来源: 互联网

标签:outlook app-config c vsto


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

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版本无关.

解决方法:

我昨天在VSTO DLL项目中遇到了相同的问题,但我仍然不明白为什么有时会丢失带有name =“ userSettings”的原因.
但我可以提供解决方案:我已经制作了一个函数,该函数将从固定的“ .dll.config”文件中丢失的XML部分(如果缺少)复制到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);
    }

标签:outlook,app-config,c,vsto
来源: https://codeday.me/bug/20191201/2083419.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有