【问题标题】:Passing Key/Value from a dictionary into a .config file c#将键/值从字典传递到 .config 文件 c#
【发布时间】:2016-04-14 10:56:46
【问题描述】:

我在下面的函数中将两个 .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);
                    }
                }
            }

        }

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

有没有比使用字典更好的方法来做到这一点?一个 更“直接”的方式?

【问题讨论】:

  • 从表面上看,您的代码看起来应该可以工作 - 它似乎有什么问题?
  • 我从未使用过 .config 文件,我不知道如何将此键/值传递到 .config 文件中。例如,我在 .config 文件中搜索字典中的键,然后更改其键。我怎样才能做到这一点?我必须像对待 .txt 一样对待 .config 吗?对不起,我有点困惑,因为我以前从未处理过这两件事
  • 嗯,它们就像字典一样,所以,虽然我不明白你为什么会有多个设置文件,但新设置肯定是你所需要的,你只需检查它们是否不同,然后将新设置复制到老了?
  • 我需要做的是:我有两个配置文件,旧版本和新版本。但是在同一个文件(旧文件)上,我无法更改某些键/值对,因为它是由用户在安装过程中配置的,无法更改,但是对于所有其他键,我需要用可以更新的新键来更新它们可以在最新的 .config 文件中找到。这能回答你的问题吗?是的:)我检查差异并复制一个,如果不存在,则添加一个新的键/值
  • 好吧,这有点道理。您的 if 可能需要更多标准,例如 if (!fixeditems.contains(newkey )) 但是,这应该可以很好地工作。记得做一个 .Save() 之后!

标签: c# dictionary key config


【解决方案1】:
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!";
                }

        }

事后做了一些工作,这段代码就像一个魅力

【讨论】:

    猜你喜欢
    • 2016-08-04
    • 1970-01-01
    • 2022-08-09
    • 2012-12-10
    • 1970-01-01
    • 2021-09-21
    • 2014-01-01
    • 2023-03-24
    • 1970-01-01
    相关资源
    最近更新 更多