gpt4 book ai didi

c# - 如何合并两个 NameValueCollection?

转载 作者:太空狗 更新时间:2023-10-30 00:24:12 24 4
gpt4 key购买 nike

我有两个 NameValueCollection:

NameValueCollection customTag = (NameValueCollection)System.Configuration.ConfigurationManager.GetSection("secureAppSettings");
NameValueCollection appSetting = (NameValueCollection)System.Configuration.ConfigurationManager.GetSection("appSettings");

我尝试了 customTag.Add(appSetting); 方法,但出现此错误:Collection is read-only。

我如何将它们合并为一个,以便我可以访问两者的所有元素?

最佳答案

要合并集合,请尝试以下操作:

var secureSettings = (NameValueCollection)System.Configuration.ConfigurationManager.GetSection("secureAppSettings");
var appSettings = (NameValueCollection)System.Configuration.ConfigurationManager.AppSettings;

// Initialise a new NameValueCollection with the contents of the secureAppSettings section
var allSettings = new NameValueCollection(secureSettings);
// Add the values from the appSettings section
foreach (string key in appSettings)
{
// Overwrite any entry already there
allSettings[key] = appSettings[key];
}

使用新的 allSettings 集合访问组合设置。

关于c# - 如何合并两个 NameValueCollection?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28055627/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com