gpt4 book ai didi

c# - 写入resx文件c#

转载 作者:行者123 更新时间:2023-11-30 14:50:16 27 4
gpt4 key购买 nike

尝试写入以 xml 编写的 Resx 文件。

我将如何向其添加列和行。

List<string> _paths = new List<string> { ConfigurationManager.AppSettings["SpanPath"], ConfigurationManager.AppSettings["FrenPath"], ConfigurationManager.AppSettings["RusPath"] };

ResourceWriter resourceWriter = new ResourceWriter(_paths.ElementAt(0));

resourceWriter.AddResource("Key1", "String1");
resourceWriter.AddResource("Key2", "String2");
resourceWriter.Close();

我想添加 key1 并在该行旁边的列中包含 string1 等等。

我想我不明白 msdn 是如何解释资源编写器的使用方式的。

最佳答案

您缺少 resourceWriter.Generate() 调用。

List<string> _paths = new List<string> { ConfigurationManager.AppSettings["SpanPath"], ConfigurationManager.AppSettings["FrenPath"], ConfigurationManager.AppSettings["RusPath"] };

using(ResXResourceWriter resourceWriter = new ResXResourceWriter(_paths.ElementAt(0)))
{
resourceWriter.AddResource("Key1", "String1");
resourceWriter.AddResource("Key2", "String2");
resourceWriter.Generate();
}

编辑。如果您丢失了旧 key ,您可以将它们存储在哈希表中,将新 key 添加到哈希表中并从哈希表中重新生成 resx。

using System.Resources;

List<string> _paths = new List<string> { ConfigurationManager.AppSettings["SpanPath"], ConfigurationManager.AppSettings["FrenPath"], ConfigurationManager.AppSettings["RusPath"] };

Hashtable oHt = new Hashtable();

// Read the keys and store in a hash table
using (ResXResourceReader oReader = new ResXResourceReader(_paths.ElementAt(0)))
{
IDictionaryEnumerator oResource = oReader.GetEnumerator();
while (oResource.MoveNext())
oHt.Add(oResource.Key,oResource.Value);
}

//Add the new keys to the hash table
oHt["Key1"] = "String1";
oHt["Key2"] = "String2";

//Re-generate the new resx from the hash table
using (ResXResourceWriter oWriter = new ResXResourceWriter(_paths.ElementAt(0)))
{
foreach (string key in oHt.Keys)
oWriter.AddResource(key.ToString(), oHt[key].ToString());
oWriter.Generate();
}

关于c# - 写入resx文件c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37080039/

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