资源 (.resx) 数据未保存
Resource (.resx) data is not saved
我不明白问题出在哪里。请检查我的代码片段。每次我添加资源数据时,它都会清除最后的数据并在 .resx 中写入新记录。
例如,Applications.resx 具有 "MyApp1" 键和 "MyApp1Path" 值。下次如果我添加 "MyApp2" 键和 "MyApp2Path" 值,我注意到 {"MyApp1", "MyApp1Path"} 不存在。
//Adding Application in Applications List
ResourceHelper.AddResource("Applications", _appName, _appPath);
这是 ResourceHelper class:
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);
}
}
免责声明,未经测试,可能需要错误检查
我不明白问题出在哪里。请检查我的代码片段。每次我添加资源数据时,它都会清除最后的数据并在 .resx 中写入新记录。 例如,Applications.resx 具有 "MyApp1" 键和 "MyApp1Path" 值。下次如果我添加 "MyApp2" 键和 "MyApp2Path" 值,我注意到 {"MyApp1", "MyApp1Path"} 不存在。
//Adding Application in Applications List
ResourceHelper.AddResource("Applications", _appName, _appPath);
这是 ResourceHelper class:
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);
}
}
免责声明,未经测试,可能需要错误检查