从嵌入资源文件创建 ResXResourceSet 对象时出现问题
我正在尝试打开一个 resx 文件,该文件是我的 C# 项目中的资源。我需要创建一个 ResXResourceSet 对象。但是在运行时会抛出“路径中的非法字符”异常。这是我尝试使用的代码。
var resX = new ResXResourceSet(Project.Properties.Resources.ResXFile);
ResXResourceSet 类只有两个构造函数(来自流和文件名)。在这种情况下如何创建 ResXResourceSet 类的对象?
I am trying to open a resx file that is a resource in my C# project. I need to create a ResXResourceSet object. However at runtime an "Illegal characters in path" exception is thrown. This is the code I am trying to use.
var resX = new ResXResourceSet(Project.Properties.Resources.ResXFile);
The ResXResourceSet class has only two constructors (from stream and from file name). How can I create an object of the ResXResourceSet class in this situation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用Project.Properties.Resources.ResourceManager.GetStream("ResXFile");
如果我理解正确的话,ResXFile中的值是一个包含ResX完整内容的字符串,而不是文件路径,这是 ResXResourceSet 当您向其传递字符串时所期望的。您需要在它周围包裹一个流。
请参阅此问题以从字符串获取流:如何生成流
另外,如果将资源文件制作成项目项,就像主资源一样,您可以通过其 ResourceManager 访问其 ResourceSet:
ResXFile.ResourceManager.GetResourceSet()
可以通过右键单击项目 > 将 ResX 添加到您的项目中添加>新商品>资源文件。
UseProject.Properties.Resources.ResourceManager.GetStream("ResXFile");
If I understand correctly, the value in ResXFile is a string with the complete contents of the ResX, and not a file path, which is what ResXResourceSet expects when you pass it a string. You'll need to wrap a stream around it.
See this question for getting a stream from a string: how to generate a stream from a string?
Also, if you make the resource file into a project item, like the main resources, you can access its ResourceSet through its ResourceManager:
ResXFile.ResourceManager.GetResourceSet()
You can add a ResX to your project by rightclicking on the project > Add > New Item > Resources File.