如何以编程方式枚举resx文件中的资源?

时间:2011-01-11 11:02:53

标签: .net resources

使用Visual Studio 2010

如何枚举位于程序集内的特定资源文件(.resx)中的所有资源?

在我的项目中,我有一个包含3个文本文件的Resources.resx文件(字符串资源)。我需要枚举这3个文件,然后使用它们的名字来获取它们的内容。

感谢。

2 个答案:

答案 0 :(得分:5)

使用System.Windows.Forms中的ResXResourceReader

  

它枚举XML资源(.resx)   文件和流,并读取   顺序资源名称和值   对

public static void Main()
{

  // Create a ResXResourceReader for the file items.resx.
  ResXResourceReader rsxr = new ResXResourceReader("items.resx");

  // Iterate through the resources and display the contents to the console.
  foreach (DictionaryEntry d in rsxr)
  {
      Console.WriteLine(d.Key.ToString() + ":\t" + d.Value.ToString());
  }

 //Close the reader.
 rsxr.Close();

}

答案 1 :(得分:3)

发现:

Dim man As New ResourceManager("Discovery.Resources", Assembly.GetExecutingAssembly)
Dim resset As ResourceSet
resset = man.GetResourceSet(CultureInfo.CurrentCulture, True, True)
For Each item As DictionaryEntry In resset
    Console.WriteLine(item.Key)
Next
相关问题