2016-04-15 35 views
0

我有这个代码来从自定义.config文件中获取所有的键/值,我通过它的路径,但代码检索0键,我不明白为什么。代码不会从.config文件检索任何密钥c#

下面的代码:

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); 
} 
+1

的AppSettings或UserSettings或SomeOtherSettings?你正在检查正确的节点/设置名称?如果可能的话,请显示配置文件。 – touchofevil

+0

我想要检索appSettings部分中的所有设置。 这里是配置文件:https://drive.google.com/file/d/0ByZb1KP9efohTDVhZXp1VkNBOEk/view?usp=sharing –

+0

@touchofevil在代码中,我没有指定我想要的部分。那可以吗? –

回答

0

你可以从web.config中获取键值到您的网页如..

ConfigurationManager.AppSettings [ “SleepTimeInSeconds”];

这里“SleepTimeInSeconds”是你的钥匙&你使用的键名值

你必须写所有密钥验证码...

+0

,但我需要检索.config中的所有键和值到字典,如上所述。但代码不返回任何键或值 –

+0

我明白,但我不能继续这样做,因为我不知道文件中的所有关键名称,他们甚至可能会不同。我需要返回指定任意键的所有键 –

+0

ConfigurationManager.appSettings.Keys如何从appSettings获取所有键并迭代所有键并使用它们。 –