以编程方式修改现有资源文件

时间:2013-09-05 10:04:12

标签: c# .net vb.net resources

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

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

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

最诚挚的问候, ANKIT

3 个答案:

答案 0 :(得分:4)

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();
    }
}

答案 1 :(得分: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();

}

参考链接here

答案 2 :(得分:0)

我有同样的问题解决它:

这将附加到您现有的.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();