6

我正在使用以下代码以编程方式生成资源文件。

ResXResourceWriter resxWtr = new ResXResourceWriter(@"C:\CarResources.resx");
resxWtr.AddResource("Title", "Classic American Cars");
resxWtr.Generate();
resxWtr.Close();

现在,我想修改从上面的代码中创建的资源文件。如果我使用相同的代码,现有的资源文件将被替换。请帮我修改它而不丢失现有内容。

最好的问候, Ankit

4

3 回答 3

6
public static void AddOrUpdateResource(string key, string value)
{
    var resx = new List<DictionaryEntry>();
    using (var reader = new ResXResourceReader(resourceFilepath))
    {
        resx = reader.Cast<DictionaryEntry>().ToList();
        var existingResource = resx.Where(r => r.Key.ToString() == key).FirstOrDefault();
        if (existingResource.Key == null && existingResource.Value == null) // NEW!
        {
            resx.Add(new DictionaryEntry() { Key = key, Value = value });
        }
        else // MODIFIED RESOURCE!
        {
            var modifiedResx = new DictionaryEntry() { Key = existingResource.Key, Value = value };
            resx.Remove(existingResource);  // REMOVING RESOURCE!
            resx.Add(modifiedResx);  // AND THEN ADDING RESOURCE!
        }
    }
    using (var writer = new ResXResourceWriter(ResxPathEn))
    {
        resx.ForEach(r =>
        {
            // Again Adding all resource to generate with final items
            writer.AddResource(r.Key.ToString(), r.Value.ToString());
        });
        writer.Generate();
    }
}
于 2015-08-15T09:59:30.367 回答
1

我有同样的问题解决它:

这将附加到您现有的 .resx 文件

 var reader = new ResXResourceReader(@"C:\CarResources.resx");//same fileName
 var node = reader.GetEnumerator();
 var writer = new ResXResourceWriter(@"C:\CarResources.resx");//same fileName(not new)
 while (node.MoveNext())
         {
     writer.AddResource(node.Key.ToString(), node.Value.ToString());
       }
  var newNode = new ResXDataNode("Title", "Classic American Cars");
  writer.AddResource(newNode);
  writer.Generate();
  writer.Close();
于 2016-09-06T07:30:49.807 回答
0

这是一个可以帮助您修改资源文件的函数。

public static void UpdateResourceFile(Hashtable data, String path)
{
    Hashtable resourceEntries = new Hashtable();

    //Get existing resources
    ResXResourceReader reader = new ResXResourceReader(path);
    if (reader != null)
    {
        IDictionaryEnumerator id = reader.GetEnumerator();
        foreach (DictionaryEntry d in reader)
        {
            if (d.Value == null)
                resourceEntries.Add(d.Key.ToString(), "");
            else
                resourceEntries.Add(d.Key.ToString(), d.Value.ToString());
        }
        reader.Close();
    }

    //Modify resources here...
    foreach (String key in data.Keys)
    {
        if (!resourceEntries.ContainsKey(key))
        {

            String value = data[key].ToString();
            if (value == null) value = "";

            resourceEntries.Add(key, value);
        }
    }

    //Write the combined resource file
        ResXResourceWriter resourceWriter = new ResXResourceWriter(path);

        foreach (String key in resourceEntries.Keys)
        {
            resourceWriter.AddResource(key, resourceEntries[key]);
        }
        resourceWriter.Generate();
        resourceWriter.Close();

}

参考链接在这里

于 2013-09-05T10:33:07.943 回答