我尝试从自定义配置文件访问配置项,但收到错误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");
}
}
读取设置的功能:(在appSettings [key]上生成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>
我是编码方面的新手,我无法弄清楚代码中的问题是什么。也许有些人可以帮助我。 问候kami
我在更改某些代码时遇到的问题:
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
答案 0 :(得分:2)
编译器抱怨这一行:string result = appSettings[key] ?? string.Empty;
,具体而言,appSettings[key]
由于其保护级别而无法访问。
要从自定义.config文件中提取设置值,您可以使用appSettings实例上的Settings属性:
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;
}
}
编辑:固定代码(缺少.Value)
答案 1 :(得分:0)
这是我解决问题的方法:
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;
}
}
感谢你的帮助Steve Wong