修改在c#.resx文件 [英] Modifying .resx file in c#

查看:245
本文介绍了修改在c#.resx文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I had a .resx file contains string name value pairs. Now I want to modify the values in certain name value pairs programmatically using C#. How can I achieve that.

先谢谢了。

推荐答案

有对资源管理的整体空间:在System.Resources。退房ResourceManager类,以及ResXResourceReader和ResXResourceWriter。

There's a whole namespace for resource management: System.Resources. Check out the ResourceManager class, as well as ResXResourceReader and ResXResourceWriter.

<一个href=\"http://msdn.microsoft.com/en-us/library/system.resources.aspx\">http://msdn.microsoft.com/en-us/library/system.resources.aspx


我设法把我的手,我用在一个点用时,我在测试一些资源相关的东西一个非常古老的调试方法。这应该做的伎俩给你。

I managed to lay my hands on a very old debug method that I used to use at one point when I was testing some resource related stuff. This should do the trick for you.

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

    }

这篇关于修改在c#.resx文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆