访问自定义配置文件 (CS0122) 中的应用设置

本文关键字:应用 设置 CS0122 自定义 配置文件 访问 | 更新日期: 2023-09-27 18:36:26

我正在尝试从自定义配置文件访问配置项,但收到错误CS0122。

类:

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

写入设置的功能:(这运行良好)

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

读取设置的功能:(这会在应用程序设置[键]上生成CS0122)

    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文件:

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

我是编码新手,我无法弄清楚我的代码中有什么问题。也许有些人可以帮助我。问候卡米

我在更改某些代码时遇到的问题:

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

访问自定义配置文件 (CS0122) 中的应用设置

编译器抱怨这一行: string result = appSettings[key] ?? string.Empty; ,具体来说,appSettings[key]由于其保护级别而无法访问。

若要从自定义 .config 文件中提取设置值,可以使用 appSettings 实例上的"设置"属性:

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

编辑:修复代码(缺少.值)

这是我对这个问题的解决方案:

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

感谢您的帮助 史蒂夫·黄