【发布时间】:2010-01-11 09:53:56
【问题描述】:
有没有办法在 C# 中循环遍历 .resx 文件中的所有资源?
【问题讨论】:
-
您能否详细说明 RESX 文件是否在您的项目内部,或者您是否想要(或需要)读取单独的 RESX 文件或从另一个程序集读取 RESX?
标签: c# .net embedded-resource
有没有办法在 C# 中循环遍历 .resx 文件中的所有资源?
【问题讨论】:
标签: c# .net embedded-resource
您应该始终使用资源管理器而不是直接读取文件,以确保将全球化考虑在内。
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;
}
【讨论】:
ResourceManager MyResourceClass = new ResourceManager("Resources.ResourceFileName", System.Reflection.Assembly.Load("App_GlobalResources"));
关于它的博客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 类的其他一些静态方法即可获得不同的程序集对象。希望对你有帮助:)
【讨论】:
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());
}
【讨论】:
在您将资源 .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文件在当前程序集或项目范围内时可用。否则,使用“脉冲”提供的方法。
此方法的优点是您可以调用为您提供的实际属性,如果您愿意,可以考虑任何本地化。但是,它相当多余,因为通常您应该使用类型安全的直接方法来调用资源的属性。
【讨论】:
// 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
【讨论】:
System.Windows.Forms 程序集中,如果您使用的是 MVC 应用程序,则不会自动添加
【讨论】:
如果您想使用 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);
}
【讨论】:
对于 nuget 包 System.Resources.ResourceManager (v4.3.0),ResourceSet 和 ResourceManager.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;
}
}
【讨论】:
简单的读取循环使用此代码
var resx = ResourcesName.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, false, false);
foreach (DictionaryEntry dictionaryEntry in resx)
{
Console.WriteLine("Key: " + dictionaryEntry.Key);
Console.WriteLine("Val: " + dictionaryEntry.Value);
}
【讨论】:
使用LINQ to SQL:
XDocument
.Load(resxFileName)
.Descendants()
.Where(_ => _.Name == "data")
.Select(_ => $"{ _.Attributes().First(a => a.Name == "name").Value} - {_.Value}");
【讨论】: