将字典中的键/值传递到.config文件中c#

时间:2016-04-14 10:56:46

标签: c# dictionary key config

我创建了下面的函数,我将两个.config文件中的密钥加载到两个字典中,然后比较这些字典,如果它们不同则更改值,或者如果它不存在则添加新的键/值对。 但现在我需要再次将它们添加到.config文件中,替换旧值和/或添加新对。 我可以这样做,如果它是.txt还是有更好的方法? 这是功能:

 public void UpdateClient(string FilePathOld, string FilePathNew)
        {
            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 = settings.AllKeys.ToDictionary(key => key, key => settings[key].Value);

            //Old = (config.GetSection("<appSettings>") as System.Collections.Hashtable)

            if (Old.Count == New.Count) // Require equal count.
            {

                foreach (var NewKey in New)
                {
                    string value;
                    if (Old.TryGetValue(NewKey.Key, out value))
                    {
                        if (value != NewKey.Value)
                        {
                            Old[NewKey.Key] = NewKey.Value;
                        }
                    }
                    else
                    {
                        Old.Add(NewKey.Key, NewKey.Value);
                    }
                }
            }

        }

任何帮助将不胜感激。谢谢

  

有没有比使用词典更好的方法呢?一个   更“直接”的方式?

1 个答案:

答案 0 :(得分:0)

public void UpdateService(string FilePathOld, string FilePathNew, string LatestVersion)
        {
                Dictionary<string, string> Old = new Dictionary<string, string>();
                Dictionary<string, string> New = new Dictionary<string, string>();

                if (ExisteFicheiro(FilePathNew) == true && ExisteFicheiro(FilePathOld) == true)
                {
                    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);

                    foreach (var NewKey in New)
                    {
                        string value;
                        if (Old.TryGetValue(NewKey.Key, out value))
                        {
                            if (value != NewKey.Value)
                            {
                                //if (ExistsKey(NewKey.Key, false) == true)
                                Old[NewKey.Key] = NewKey.Value;

                            }
                        }
                        else
                        {
                            Old.Add(NewKey.Key, NewKey.Value);
                        }
                    }

                    foreach (var NewKey in Old)
                    {
                        string key = NewKey.Key;
                        string value = NewKey.Value;
                        if (config.AppSettings.Settings[key] != null)
                        {
                            config.AppSettings.Settings[key].Value = value;
                            if (key == "Version")
                                config.AppSettings.Settings[key].Value = LatestVersion;
                        }
                        else
                        {
                            config.AppSettings.Settings.Add(key, value);

                        }
                        if (config.AppSettings.Settings["Version"] == null)
                        {
                            config.AppSettings.Settings.Add("Version", LatestVersion);
                        }

                    }
                    config.Save();
                }
                else
                {
                    Erro NovoErro = new Erro();
                    Global.Erro = "O ficheiro \"OrcaService.exe.config\" ou o ficheiro \"Orca.exe.config\" não existem nos caminhos especificados!";
                }

        }

完成一些后续工作,这段代码就像一个魅力