当前位置:  开发笔记 > 编程语言 > 正文

在C#中修改.resx文件

如何解决《在C#中修改.resx文件》经验,为你挑选了3个好方法。

我有一个.resx包含名称 - 值对(两个字符串)的文件.现在我想使用C#以编程方式修改某些名称 - 值对中的值.我怎样才能做到这一点?



1> womp..:

资源管理有一个完整的命名空间:System.Resources.查看ResourceManager类,以及ResXResourceReader和ResXResourceWriter.

http://msdn.microsoft.com/en-us/library/system.resources.aspx


我设法把手放在一个非常古老的调试方法上,当我测试一些与资源相关的东西时,我常常使用它.这应该为你做的伎俩.

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

    }



2> Elias Hossai..:
    public static void AddOrUpdateResource(string key, string value)
    {
        var resx = new List();
        using (var reader = new ResXResourceReader(resourceFilepath))
        {
            resx = reader.Cast().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();
        }
    }



3> Ers..:

如果要保留资源文件中的现有注释,请使用此(基于SirMoreno的代码修改)

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

        //Get existing resources
        ResXResourceReader reader = new ResXResourceReader(path);
        reader.UseResXDataNodes = true;
        ResXResourceWriter resourceWriter = new ResXResourceWriter(path);
        System.ComponentModel.Design.ITypeResolutionService typeres = null;
        if (reader != null)
        {
            IDictionaryEnumerator id = reader.GetEnumerator();
            foreach (DictionaryEntry d in reader)
            {
                //Read from file:
                string val = "";
                if (d.Value == null)
                    resourceEntries.Add(d.Key.ToString(), "");
                else
                {
                    val = ((ResXDataNode)d.Value).GetValue(typeres).ToString();
                    resourceEntries.Add(d.Key.ToString(), val);

                }

                //Write (with read to keep xml file order)
                ResXDataNode dataNode = (ResXDataNode)d.Value;

                //resourceWriter.AddResource(d.Key.ToString(), val);
                resourceWriter.AddResource(dataNode);

            }
            reader.Close();
        }

        //Add new data (at the end of the file):
        Hashtable newRes = new Hashtable();
        foreach (String key in data.Keys)
        {
            if (!resourceEntries.ContainsKey(key))
            {

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

                resourceWriter.AddResource(key, value);
            }
        }

        //Write to file
        resourceWriter.Generate();
        resourceWriter.Close();

    }

推荐阅读
女女的家_747
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有