How to read an App.Config of a different application in C#

不打扰是莪最后的温柔 提交于 2019-12-12 12:22:58

问题


I have a console application written in C# which uses an app.config file. This application is intended to be run on a server using the task scheduler. Now I want to develop a UI that reads and writes from and to the app.config. (Note that this config is not intended to replace the config file of the UI application.)

But I'm struggling to read the settings from the file. Using the ConfigurationManager I'm able to open the config file, BUT I cannot access the configuration settings.

This is the sample config file generated by Visual Studio (2010):

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="AccessingConfigSample.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <userSettings>
        <AccessingConfigSample.Properties.Settings>
            <setting name="ApplicationTitle" serializeAs="String">
                <value>Accessing Config files</value>
            </setting>
            <setting name="VersionNo" serializeAs="String">
                <value>V 1.0</value>
            </setting>
        </AccessingConfigSample.Properties.Settings>
    </userSettings>
</configuration>

After consulting several article on stackoverflow, I tried this to open the file and access the user section:

if (File.Exists(configFile))
{
   var configMap = new ExeConfigurationFileMap{ ExeConfigFilename = configFile};
   var config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
   var userSection = config.GetSection("userSettings");
}

I tried this as well:

var userSection = config.GetSection("AccessingConfigSample.Properties.Settings");

Both returned null.

So what am I doing wrong here?

Any help or hints are highly appreciated!


回答1:


The config file you use as an example is using a ConfigurationSectionGroup and those need to be read with the matching method GetSectionGroup on the Configuration element instead of GetSection

The following code snippet does output the content of the SectionGroup to the Debug console:

if (File.Exists(configFile))
{
    var configMap = new ExeConfigurationFileMap { ExeConfigFilename = configFile };
    var config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
    // get the sectionGroup!
    var userSectionGroup = config.GetSectionGroup("userSettings");
    foreach (var userSection in userSectionGroup.Sections)
    {
        // check for a ClientSettingSection
        if (userSection is ClientSettingsSection)
        {
            // cast from ConfigSection to a more specialized type
            var clientSettingSect = (ClientSettingsSection) userSection;
            foreach (SettingElement clientSetting in clientSettingSect.Settings)
            {
                Debug.WriteLine(String.Format("{0}={1}", clientSetting.Name, clientSetting.Value.ValueXml.InnerText ));
            }
        }

    }
}

Notice that I cast the object instance to a ClientSettingSection to retrieve the settings value (which is a SettingElement).

If you put this to work with the sample config you provided the result in the Debug Output Window pane should be:

ApplicationTitle=Accessing Config files
VersionNo=V 1.0


来源:https://stackoverflow.com/questions/28542432/how-to-read-an-app-config-of-a-different-application-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!