131

有没有办法遍历.resxC# 文件中的所有资源?

4

10 回答 10

253

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

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

...

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

ResourceSet resourceSet =
    MyResourceClass.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
foreach (DictionaryEntry entry in resourceSet)
{
    string resourceKey = entry.Key.ToString();
    object resource = entry.Value;
}
于 2010-01-11T10:05:49.513 回答
27

在我的博客上写博客:)简短版本是,查找资源的全名(除非你已经知道它们):

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该类的其他一些静态方法来获得不同的程序集对象。希望能帮助到你 :)

于 2010-01-11T10:07:45.033 回答
9

使用ResXResourceReader 类

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());
}
于 2010-01-11T10:00:14.530 回答
7

在您将资源 .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 文件在当前程序集或项目范围内时可用。否则,使用“脉冲”提供的方法。

这种方法的优点是您可以调用为您提供的实际属性,如果您愿意,可以考虑任何本地化。但是,它相当多余,因为通常您应该使用类型安全的直接方法来调用资源的属性。

于 2010-01-11T10:09:25.120 回答
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();

见链接:微软示例

于 2013-03-27T08:32:09.117 回答
2

您可以使用ResourceManager.GetResourceSet

于 2010-01-11T10:01:35.183 回答
2

如果要使用 LINQ,请使用resourceSet.OfType<DictionaryEntry>(). 例如,使用 LINQ 可以让您根据索引 (int) 而不是键 (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);
}
于 2017-10-18T14:33:40.567 回答
1

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

使用ResourceReader, 正如这篇文章所建议的那样:“ C# - 无法从 ResourceManager 获取字符串(来自卫星程序集)

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

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;
   }
}
于 2018-07-03T06:29:53.530 回答
1

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

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

foreach (DictionaryEntry dictionaryEntry in resx)
{
    Console.WriteLine("Key: " + dictionaryEntry.Key);
    Console.WriteLine("Val: " + dictionaryEntry.Value);
}
于 2020-02-19T08:18:36.010 回答
0

使用LINQ to SQL

XDocument
        .Load(resxFileName)
        .Descendants()
        .Where(_ => _.Name == "data")
        .Select(_ => $"{ _.Attributes().First(a => a.Name == "name").Value} - {_.Value}");
于 2016-07-04T10:22:29.470 回答