2016-04-15 12 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からすべてのキーを取得し、すべてのキーを繰り返して使用します。 –

関連する問題