kami
kami

Reputation: 274

Accessing AppSettings in Custom.Configuration File (CS0122)

I'm trying to access configuration-items from a custom configuration file but getting the error CS0122.

Class:

using System;
using System.IO;
using System.Configuration;
using System.Windows.Forms;

public class SaveLoadSettings
{
    private static string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "XMLConverterForTelekomInvoice", "XMLConverterForTelekomInvoice.config");
}

Function to write settings: (this is working well)

    private static void AddUpdateAppSettings(string key, string value)
    {
        try
        {
            var configFile = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap { ExeConfigFilename = path }, ConfigurationUserLevel.None);
            var settings = configFile.AppSettings.Settings;
            if (settings[key] == null)
            {
                settings.Add(key, value);
            }
            else
            {
                settings[key].Value = value;
            }
            configFile.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);
        }
        catch (ConfigurationErrorsException)
        {
            MessageBox.Show("Error writing app settings");
        }
    }

Function to read settings: (This generates CS0122 on appSettings[key])

    private static string ReadSetting(string key)
    {
        try
        {
            Configuration MyAppConfig = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap { ExeConfigFilename = path }, ConfigurationUserLevel.None);
            var appSettings = MyAppConfig.AppSettings;
            string result = appSettings[key] ?? string.Empty;
            return result;
        }
        catch (ConfigurationErrorsException)
        {
            MessageBox.Show("Error reading app settings");
            return string.Empty;
        }
    }

XML File:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <appSettings>
        <add key="Setting1" value="25" />
        <add key="Setting2" value="10" />
    </appSettings>
</configuration>

I'm a rookie in coding and I can't figure out what's the problem in my code. Maybe some can help me. Greetings kami

Problems I ran in changing some code:

string result = appSettings.Settings[key] ?? string.Empty; // Generates CS0019 ?? Operator can't be used on KeyValueConfigurationElement
string result = appSettings.Settings[key].ToString() ?? string.Empty; // Throws "System.NullReferenceException" in Runtime

Upvotes: 3

Views: 2258

Answers (2)

kami
kami

Reputation: 274

This is my solution for the problem:

private static string ReadSettingString(string key)
{
    try
    {
        Configuration MyAppConfig = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap { ExeConfigFilename = path }, ConfigurationUserLevel.None);
        var appSettings = MyAppConfig.AppSettings;
        string result = appSettings?.Settings?[key]?.Value ?? string.Empty;
        return result;
    }
    catch (ConfigurationErrorsException)
    {
        MessageBox.Show("Error reading app settings");
        return string.Empty;
    }
}

Thanks for your help Steve Wong

Upvotes: 0

Steve Wong
Steve Wong

Reputation: 2256

The compiler is complaining about this line: string result = appSettings[key] ?? string.Empty;, specifically, that appSettings[key] is not accessible due to it's protection level.

To extract the setting value from your custom .config file, you can use the Settings property on the appSettings instance:

private static string ReadSetting(string key)
    {
        try
        {
            Configuration MyAppConfig = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap { ExeConfigFilename = path }, ConfigurationUserLevel.None);
            var appSettings = MyAppConfig.AppSettings;
            string result = appSettings.Settings[key].Value ?? string.Empty; // ** change here
            return result;
        }
        catch (ConfigurationErrorsException)
        {
            MessageBox.Show("Error reading app settings");
            return string.Empty;
        }
    }

Edit: fixed code (missing .Value)

Upvotes: 2

Related Questions