循环遍历.resx文件中的所有资源

时间:2010-01-11 09:53:56

标签: c# .net embedded-resource

有没有办法循环遍历C#中的.resx文件中的所有资源?

10 个答案:

答案 0 :(得分:232)

您应该始终使用资源管理器而不是直接读取文件以确保考虑全球化。

using System.Collections;
using System.Globalization;
using System.Resources;

...


        ResourceManager MyResourceClass = new ResourceManager(typeof(Resources /* Reference to your resources class -- may be named differently in your case */));

ResourceSet resourceSet = MyResourceClass.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
foreach (DictionaryEntry entry in resourceSet)
{
    string resourceKey = entry.Key.ToString();
    object resource = entry.Value;
}

答案 1 :(得分:26)

Blogged about it on my blog :)短版本是,找到资源的全名(除非你已经知道):

var assembly = Assembly.GetExecutingAssembly();

foreach (var resourceName in assembly.GetManifestResourceNames())
    System.Console.WriteLine(resourceName);

将所有这些用于某事:

foreach (var resourceName in assembly.GetManifestResourceNames())
{
    using(var stream = assembly.GetManifestResourceStream(resourceName))
    {
        // Do something with stream
    }
}

要使用除执行程序之外的其他程序集中的资源,您只需使用Assembly类的其他一些静态方法获取不同的程序集对象。希望它有所帮助:)

答案 2 :(得分:8)

使用ResXResourceReader Class

ResXResourceReader rsxr = new ResXResourceReader("your resource file path");

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

答案 3 :(得分:7)

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

  // Create an IDictionaryEnumerator to iterate through the resources.
  IDictionaryEnumerator id = rsxr.GetEnumerator();       

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

请参阅链接:microsoft example

答案 4 :(得分:5)

在将资源.RESX文件添加到项目中的那一刻,Visual Studio将创建一个具有相同名称的Designer.cs,为您创建一个类,其中所有资源项都是静态属性。在键入资源文件的名称后,在编辑器中键入点时,可以看到资源的所有名称。

或者,您可以使用反射来遍历这些名称。

Type resourceType = Type.GetType("AssemblyName.Resource1");
PropertyInfo[] resourceProps = resourceType.GetProperties(
    BindingFlags.NonPublic | 
    BindingFlags.Static | 
    BindingFlags.GetProperty);

foreach (PropertyInfo info in resourceProps)
{
    string name = info.Name;
    object value = info.GetValue(null, null);  // object can be an image, a string whatever
    // do something with name and value
}

此方法显然仅在RESX文件位于当前程序集或项目的范围内时才可用。否则,请使用“pulse”提供的方法。

此方法的优点是您可以调用已为您提供的实际属性,并根据需要考虑任何本地化。但是,它相当多余,因为通常您应该使用类型安全的直接方法来调用资源的属性。

答案 5 :(得分:2)

您可以使用ResourceManager.GetResourceSet

答案 6 :(得分:1)

如果您想使用LINQ,请使用resourceSet.OfType<DictionaryEntry>()。例如,使用LINQ可以根据索引(int)而不是key(string)选择资源:

ResourceSet resourceSet = Resources.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
foreach (var entry in resourceSet.OfType<DictionaryEntry>().Select((item, i) => new { Index = i, Key = item.Key, Value = item.Value }))
{
    Console.WriteLine(@"[{0}] {1}", entry.Index, entry.Key);
}

答案 7 :(得分:1)

使用nuget软件包System.Resources.ResourceManager(v4.3.0),ResourceSetResourceManager.GetResourceSet不可用。

使用ResourceReader,如本文所建议:“ C# - Cannot getting a string from ResourceManager (from satellite assembly)

仍然可以读取资源文件的键/值。

System.Reflection.Assembly resourceAssembly = System.Reflection.Assembly.Load(new System.Reflection.AssemblyName("YourAssemblyName"));
String[] manifests = resourceAssembly.GetManifestResourceNames(); 
using (ResourceReader reader = new ResourceReader(resourceAssembly.GetManifestResourceStream(manifests[0])))
{
   System.Collections.IDictionaryEnumerator dict = reader.GetEnumerator();
   while (dict.MoveNext())
   {
      String key = dict.Key as String;
      String value = dict.Value as String;
   }
}

答案 8 :(得分:0)

使用LINQ to SQL

XDocument
        .Load(resxFileName)
        .Descendants()
        .Where(_ => _.Name == "data")
        .Select(_ => $"{ _.Attributes().First(a => a.Name == "name").Value} - {_.Value}");

答案 9 :(得分:0)

简单的读取循环使用此代码

var resx = ResourcesName.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, false, false);

foreach (DictionaryEntry dictionaryEntry in resx)
{
    Console.WriteLine("Key: " + dictionaryEntry.Key);
    Console.WriteLine("Val: " + dictionaryEntry.Value);
}