不保存资源 (.resx) 数据



我无法理解问题所在。请检查我的代码片段。每次添加资源数据时,它都会清除最后一个数据并在 .resx 中写入新记录。 例如,Applications.resx 具有具有"MyApp1Path"值的"MyApp1"键。下次如果我添加带有"MyApp2Path"值的"MyApp2"键,我注意到{"MyApp1","MyApp1Path"}不存在。

//Adding Application in Applications List
ResourceHelper.AddResource("Applications", _appName, _appPath);

下面是 ResourceHelper 类:

public class ResourceHelper
{
public static void AddResource(string resxFileName, string name, string value)
{
using (var resx = new ResXResourceWriter(String.Format(@".Resources{0}.resx", resxFileName)))
{
resx.AddResource(name, value);
}
}
}

是的,这是意料之中的,ResXResourceWriter只是添加节点,它不会追加。

但是,您可以读出节点,然后再次添加它们

public static void AddResource(string resxFileName, string name, object value)
{
var fileName = $@".Resources{resxFileName}.resx";
using (var writer = new ResXResourceWriter(fileName))
{
if (File.Exists(fileName))
{
using (var reader = new ResXResourceReader(fileName))
{
var node = reader.GetEnumerator();
while (node.MoveNext())
{
writer.AddResource(node.Key.ToString(), node.Value);
}
}
}
writer.AddResource(name, value);
}
}

免责声明,未经测试,可能需要错误检查

最新更新