Accessing AppSettings in Custom.Configuration File (CS0122)
<p>I'm trying to access configuration-items from a custom configuration file but getting the error CS0122.</p>

<p>Class:</p>

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

<p>Function to write settings: (this is working well)</p>

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

<p>Function to read settings: (This generates CS0122 on appSettings[key])</p>

<pre><code>    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;
        }
    }
</code></pre>

<p>XML File:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;configuration&gt;
    &lt;appSettings&gt;
        &lt;add key="Setting1" value="25" /&gt;
        &lt;add key="Setting2" value="10" /&gt;
    &lt;/appSettings&gt;
&lt;/configuration&gt;
</code></pre>

<p>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</p>

<p>Problems I ran in changing some code:</p>

<pre><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
</code></pre>