如何在C#中迭代Resources.resx?

huangapple go评论93阅读模式
英文:

How to iterate over Resources.resx in C#?

问题

我想在C#中访问多个文件,但我没有成功地在没有硬编码路径的情况下做到这一点。我不想指定确切的路径,因为程序应该独立于其位置工作。
我认为我应该通过将文件添加到资源中来实现这一点,但我找不到如何遍历这些文件。我找到了几个关于读取 .resx 文件的页面,但似乎都只涉及按名称访问一个特定资源,或者使用了硬编码的路径。
我目前的代码如下:

ResXResourceReader resourcesreader = new ResXResourceReader(Properties.Resources);

这会导致编译器错误:“'Resources' 是一个类型,在给定上下文中无效”。

当我硬编码一个路径时,奇怪的是,我在运行时会收到错误消息。

ResXResourceReader resourcesreader = new ResXResourceReader(@"G:\Programming\C#\Contest Judging\Contest Judging\Properties\Resources.resx");
foreach (DictionaryEntry image in resourcesreader)

在底部一行,会引发异常:

System.ArgumentException
  ResX file Could not find a part of the path 'G:\Programming\C#\Contest Judging\Contest Judging\bin\Resources\BugsyWPfeiffer 1.png'. Line 123, position 5. 无法解析。
  
Inner Exception 1:
XmlException: Could not find a part of the path 'G:\Programming\C#\Contest Judging\Contest Judging\bin\Resources\BugsyWPfeiffer 1.png'. Line 123, position 5.

Inner Exception 2:
DirectoryNotFoundException: Could not find a part of the path 'G:\Programming\C#\Contest Judging\Contest Judging\bin\Resources\BugsyWPfeiffer 1.png'.

我想知道为什么它开始在 bin\ 中查找,因为我已经在 Resources.resx 中进行了 Ctrl + F 搜索,但并未找到相关内容。

英文:

I want to access a number of files in C#, and I haven't been able to succeed in doing so without hard-coding a path. I do not want to specify an exact path, as the program should work independent of its location.<br>
I figured I should do this by adding the files to Resources, but I can't find how to iterate over those. I found several pages about reading .resx, but it seemed like all either only addressed accessing one specific resource by name, or used a hard-coded path.<br>
The code I have currently is as follows:

ResXResourceReader resourcesreader = new ResXResourceReader(Properties.Resources);

This gives the compiler error "'Resources' is a type, which is not valid in the given context".

When I do hard-code a path, curiously, I get an error at runtime.

ResXResourceReader resourcesreader = new ResXResourceReader(@&quot;G:\Programming\C#\Contest Judging\Contest Judging\Properties\Resources.resx&quot;);
foreach (DictionaryEntry image in resourcesreader)

At the bottom line, an exception is raised:

System.ArgumentException
  ResX file Could not find a part of the path &#39;G:\Programming\C#\Contest Judging\Contest Judging\bin\Resources\BugsyWPfeiffer 1.png&#39;. Line 123, position 5. cannot be parsed.
  
Inner Exception 1:
XmlException: Could not find a part of the path &#39;G:\Programming\C#\Contest Judging\Contest Judging\bin\Resources\BugsyWPfeiffer 1.png&#39;. Line 123, position 5.

Inner Exception 2:
DirectoryNotFoundException: Could not find a part of the path &#39;G:\Programming\C#\Contest Judging\Contest Judging\bin\Resources\BugsyWPfeiffer 1.png&#39;.

I wonder why it starts looking in bin\, as I did a Ctrl + F through Resources.resx and it does not occur.

答案1

得分: 1

你可以通过从生成的 Resources 类中获取 ResourceManager 来实现这一点:

// 如果要获取特定文化的资源,请更改此处的 culture
var culture = CultureInfo.InvariantCulture;

var resourceManager = Properties.Resources.ResourceManager;
var resourceSet = resourceManager.GetResourceSet(culture, createIfNotExists: true, tryParents: true);
foreach (DictionaryEntry entry in resourceSet)
{
    Console.WriteLine($"{entry.Key}: {entry.Value}");
}

这些资源已经编译到您的应用程序中(或者编译到卫星程序集中),因此没有 resx 文件供您加载。

英文:

You can do this by fetching the ResourceManager from the generated Resources class:

// Change this if you want to use fetch the resources for a specific culture
var culture = CultureInfo.InvariantCulture;

var resourceManager = Properties.Resources.ResourceManager;
var resourceSet = resourceManager.GetResourceSet(culture, createIfNotExists: true, tryParents: true);
foreach (DictionaryEntry entry in resourceSet)
{
    Console.WriteLine($&quot;{entry.Key}: {entry.Value}&quot;);
}

The resources are compiled into your application (or into satellite assemblies), so there's no resx file for you to load.

huangapple
  • 本文由 发表于 2020年1月4日 00:52:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582334.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定