当前位置: 动力学知识库 > 问答 > 编程问答 >

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

问题描述:

I have this code to get all the key/value from a custom .config file where I pass it's path, but the code retrieves 0 keys, I'm not understanding why.

Here's the code:

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

}

网友答案:

you can access key value from web.config to your page like..

ConfigurationManager.AppSettings["SleepTimeInSeconds"];

here "SleepTimeInSeconds" is your key & you get value using your key name

you have to write this code for all keys...

分享给朋友:
您可能感兴趣的文章:
随机阅读: