如何在c#中以编程方式为资源文件中的值字段设置不同的字符串值?

时间:2015-09-28 07:25:40

标签: c#

以下是我从资源文件中获取字符串值的代码

System.Resources.ResourceManager rsMgr = new System.Resources.ResourceManager("DemoResourceLocalization.ErrorMessageResource"
System.Reflection.Assembly.GetExecutingAssembly());
string value = rsMgr.GetString("PersonName");

1 个答案:

答案 0 :(得分:0)

如果要以编程方式更改资源,则应直接使用.resx文件或使用某些自定义可写资源数据库。

您可以使用ResXResourceReaderResXResourceWriter类将字符串读入字典,最后将它们写回新的.resx文件。

public static class DynamicResourceManager
{
    private static readonly Dictionary<string, string> dictionary;

    // are there any changes
    private static bool isDirty = false;

    // The current language of the resources to manage
    private static CultureInfo activeLanguage = CultureInfo.CurrentUICulture;

    public static CultureInfo ActiveLanguage
    {
        get { return activeLanguage; }
        set
        {
            if (value == null)
                throw new ArgumentNullException("value");

            if (!activeLanguage.Equals(value))
            {
                if (isDirty)
                    SaveDictionary();

                activeLanguage = value;
                LoadDictionary();
            }
        }
    }

    private static void LoadDictionary()
    {
        // The ResourceManager is case-insensitive so we are, too
        if (dictionary == null)
             dictionary = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
        else
            dictionary.Clear();

        string file = GetFileName();
        if (!File.Exists(file))
            return;

        using (IResourceReader reader = new ResXResourceReader(file))
        {
            foreach (DictionaryEntry item in reader)
            {
                string value = item.Value as string;
                if (value != null)
                    dictionary.Add(item.Key.ToString(), value);
            }
        }
    }

    // this should be called explicitly when you exit the application, but
    // is called automatically when ActiveLanguage is changed
    public static void SaveDictionary()
    {
        if (!isDirty)
            return;

        string file = GetFileName();
        string dir = Path.GetDirectoryName(file);

        if (!Directory.Exists(dir))
            Directory.CreateDirectory(dir);

        using (IResourceWriter writer = new ResXResourceWriter(file))
        {
            foreach (KeyValuePair<string, string> item in dictionary)
                writer.AddResource(item.Key, item.Value);
        }

        isDirty = false;            
    }

    private static string GetFileName()
    {
        return Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "Languages") + activeLanguage.Name + ".resx");
    }

    public static string GetString(string key)
    {
        if (key == null)
            return String.Empty;

        if (dictionary == null)
            LoadDictionary();

        string result;
        if (!dictionary.TryGetValue(key, out result))
        {
            // entry not found in this language, adding it dynamically
            dictionary.Add("NEW:" + key, key);
            isDirty = true;
        }

        return result;
    }

    public static void SetString(string key)
    {
        if (dictionary == null)
            LoadDictionary();

        dictionary[key] = value;
        isDirty = true;
    }
}

ResourceManager类不兼容:

  • 不处理文化层级(例如en-US - &gt; en - &gt; Invariant)。
  • 忽略非字符串资源