本文介绍了缺少< configSections>部署后在配置文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Update: I had a question below but actually my problem would be solved by asking a slightly different question. Why on some machines does my application throw the error:

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

与其他计算机上不同.错误也如此处所述 .NET 3.5-配置系统无法执行初始化异常是由于我的app.config顶部缺少configSections元素引起的.当然,可以通过放置此部分来解决该问题,但是由于某些原因,在我的项目解决方案中具有该部分的app.config并不是一旦部署就在appdata文件夹中创建的那个.

where as on other machines it does not. The error as described also here .NET 3.5 - Configuration system failed to initialize exception is caused by a missing configSections element at the top of my app.config. Of course, the problem would be resolved by putting this section in but for some reason the app.config in my projects solution which has this section is not the one which gets created in the appdata folder once it is deployed.

原始问题:

为什么将我的用户配置文件部署在某些计算机而不是其他计算机上时会缺少此部分?我如何确保它不丢失.

Why would my user config file be missing this section when deployed on some machines and not others? How can I ensure it is not missing.

<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加载项.

Some background. I'm deploying a vsto add-in through a click once visual studio installer on win 7 machines running outlook 2007 and 2010.

该加载项读取一些设置并将其写入app.config文件,这与exe会存储在本地用户的appdata文件夹中不同.

The add-in reads and writes some settings to the app.config file which unlike an exe gets stored in the local users appdata folder.

但是在某些计算机上,我收到一条错误消息配置系统初始化失败-System.Configuration-位于System.Configuration.ConfigurationManager.PrepareConfigSystem()"在我的情况下,这是由xml中上述缺少的元素引起的.但是,在其他计算机上,configSections也不缺少.该问题与所使用的Outlook版本无关.

On some machines however I'm getting an error "Configuration system failed to initialize - System.Configuration - at System.Configuration.ConfigurationManager.PrepareConfigSystem()"which in my case is being caused by the above missing element in the xml. However on other machines the configSections is not missing. The problem is unrelated to the Outlook version being used.

推荐答案

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

I've had the same issue in my VSTO DLL project yesterday and I still don't understand the reason why the with name="userSettings" is sometimes missing. But I can provide my solution: I've made a function which copies the missing XML part (if it's missing) from the fixed ".dll.config" file to the config file in the ROAMING directory:

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

这篇关于缺少&lt; configSections&gt;部署后在配置文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 07:12