C# 循环遍历 .resx 文件中的所有资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2041000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loop through all the resources in a .resx file
提问by np.
Is there a way to loop through all the resources in a .resx
file in C#?
有没有办法.resx
在 C# 中循环遍历文件中的所有资源?
采纳答案by Oundless
You should always use the resource manager and not read files directly to ensure globalization is taken into account.
您应该始终使用资源管理器而不是直接读取文件以确保考虑到全球化。
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;
}
回答by rahul
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());
}
回答by Oded
You can use ResourceManager.GetResourceSet.
回答by Svish
Blogged about it on my blog:) Short version is, to find the full names of the resources(unless you already know them):
博客上讲述它在我的博客:)短的版本是,寻找资源的全名(除非你已经知道它们):
var assembly = Assembly.GetExecutingAssembly();
foreach (var resourceName in assembly.GetManifestResourceNames())
System.Console.WriteLine(resourceName);
To use all of them for something:
将它们全部用于某事:
foreach (var resourceName in assembly.GetManifestResourceNames())
{
using(var stream = assembly.GetManifestResourceStream(resourceName))
{
// Do something with stream
}
}
To use resources in other assemblies than the executing one, you'd just get a different assembly object by using some of the other static methods of the Assembly
class. Hope it helps :)
要使用其他程序集中的资源而不是正在执行的程序集中,您只需使用Assembly
该类的其他一些静态方法来获得不同的程序集对象。希望能帮助到你 :)
回答by Abel
The minute you add a resource .RESX file to your project, Visual Studio will create a Designer.cs with the same name, creating a a class for you with all the items of the resource as static properties. You can see all the names of the resource when you type the dot in the editor after you type the name of the resource file.
在您将资源 .RESX 文件添加到项目的那一刻,Visual Studio 将创建一个具有相同名称的 Designer.cs,为您创建一个类,并将资源的所有项目作为静态属性。输入资源文件名后,在编辑器中输入点号就可以看到该资源的所有名称。
Alternatively, you can use reflection to loop through these names.
或者,您可以使用反射来遍历这些名称。
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
}
This method is obviously only usable when the RESX file is in scope of the current assembly or project. Otherwise, use the method provided by "pulse".
此方法显然仅在 RESX 文件在当前程序集或项目范围内时可用。否则,使用“脉冲”提供的方法。
The advantage of this method is that you call the actual properties that have been provided for you, taking into account any localization if you wish. However, it is rather redundant, as normally you should use the type safe direct method of calling the properties of your resources.
这种方法的优点是您可以调用为您提供的实际属性,如果您愿意,还可以考虑任何本地化。然而,这是相当多余的,因为通常您应该使用类型安全的直接方法来调用资源的属性。
回答by logicfalse
// 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();
see link: microsoft example
见链接:微软示例
回答by Andriy Tolstoy
Using LINQ to SQL:
使用LINQ 到 SQL:
XDocument
.Load(resxFileName)
.Descendants()
.Where(_ => _.Name == "data")
.Select(_ => $"{ _.Attributes().First(a => a.Name == "name").Value} - {_.Value}");
回答by Ron Inbar
If you want to use LINQ, use resourceSet.OfType<DictionaryEntry>()
. Using LINQ allows you, for example, to select resources based on their index (int) instead of key (string):
如果要使用 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);
}
回答by Jordy
With the nuget package System.Resources.ResourceManager
(v4.3.0) the ResourceSet
and ResourceManager.GetResourceSet
are not available.
使用 nuget 包System.Resources.ResourceManager
(v4.3.0)ResourceSet
和ResourceManager.GetResourceSet
不可用。
Using the ResourceReader
, as this post suggest: "C# - Cannot getting a string from ResourceManager (from satellite assembly)"
使用ResourceReader
,正如这篇文章所建议的:“ C# - 无法从 ResourceManager(来自附属程序集)获取字符串”
It's still possible to read the key/values of the resource file.
仍然可以读取资源文件的键/值。
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;
}
}
回答by Azhe Kun
Simple read loop use this code
简单的读取循环使用此代码
var resx = ResourcesName.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, false, false);
foreach (DictionaryEntry dictionaryEntry in resx)
{
Console.WriteLine("Key: " + dictionaryEntry.Key);
Console.WriteLine("Val: " + dictionaryEntry.Value);
}