代码不会从 .config 文件中检索任何密钥 c#

Code doesn't retrieve any key from .config file c#

我有这段代码可以从我传递路径的自定义 .config 文件中获取所有 key/value,但代码检索到 0 个键,我不明白为什么。

代码如下:

public void UpdateService(string FilePathOld, string FilePathNew)
{
    string[] Keys = { "SleepTimeInSeconds", "LogCleanInMinutes", "LogFileSize", "SqlTrans", "RemoteType", "DebugMode", "SleepError", "LogLevel", "LogSqlClient", "LogFile", "DebugRemote" };

    Dictionary<string, string> Old = new Dictionary<string, string>();
    Dictionary<string, string> New = new Dictionary<string, string>();

    ExeConfigurationFileMap configOld = new ExeConfigurationFileMap();
    configOld.ExeConfigFilename = FilePathOld;

    Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configOld, ConfigurationUserLevel.None);

    ExeConfigurationFileMap configNew = new ExeConfigurationFileMap();
    configNew.ExeConfigFilename = FilePathNew;

    Configuration config2 = ConfigurationManager.OpenMappedExeConfiguration(configNew, ConfigurationUserLevel.None);

    KeyValueConfigurationCollection settings = config.AppSettings.Settings;
    Old = settings.AllKeys.ToDictionary(key => key, key => settings[key].Value);

    KeyValueConfigurationCollection settings2 = config2.AppSettings.Settings;
    New = settings2.AllKeys.ToDictionary(key => key, key => settings2[key].Value);
}

您可以访问 web.config 中的键值到您的页面,例如..

ConfigurationManager.AppSettings["SleepTimeInSeconds"];

这里 "SleepTimeInSeconds" 是您的密钥,您可以使用密钥名称获得价值

您必须为所有键编写此代码...