无法使用 GetManifestResourceStream() 加载清单资源

发布于 2024-09-06 07:12:43 字数 1016 浏览 6 评论 0原文

我使用 XSD 创建了一个自定义配置部分。为了解析遵循这个新架构的配置文件,我使用以下内容加载资源(我的 .xsd 文件):

public partial class MonitoringConfiguration
    {
        public const string ConfigXsd = "MonitoringAPI.Configuration.MonitoringConfiguration.xsd";
        public const string ConfigSchema = "urn:MonitoringConfiguration-1.0";

        private static XmlSchemaSet xmlSchemaSet;

        static MonitoringConfiguration()
        {
            xmlSchemaSet = new XmlSchemaSet();
            Stream xsdStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(ConfigXsd);
            XmlReader schemaReader = XmlReader.Create(xsdStream);
            xmlSchemaSet.Add(ConfigSchema, schemaReader);
        }

    }

顺便说一句,我的资源是:MonitoringConfiguration.xsd。另一个分部类(代表 .xsd 文件后面的代码)的命名空间是 MonitoringAPI.Configuration

问题出在这里:

 Stream xsdStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(ConfigXsd);

xsdStream 为空,所以我猜找不到资源!但为什么?

谢谢

I've created a custom configuration section using XSD. In order to parse the config file that follows this new schema, I load the resource (my .xsd file) with this:

public partial class MonitoringConfiguration
    {
        public const string ConfigXsd = "MonitoringAPI.Configuration.MonitoringConfiguration.xsd";
        public const string ConfigSchema = "urn:MonitoringConfiguration-1.0";

        private static XmlSchemaSet xmlSchemaSet;

        static MonitoringConfiguration()
        {
            xmlSchemaSet = new XmlSchemaSet();
            Stream xsdStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(ConfigXsd);
            XmlReader schemaReader = XmlReader.Create(xsdStream);
            xmlSchemaSet.Add(ConfigSchema, schemaReader);
        }

    }

By the way my resource is: MonitoringConfiguration.xsd. And the namespace of the other partial class (that represents the code behind of the .xsd file) is MonitoringAPI.Configuration.

The problem is situated here:

 Stream xsdStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(ConfigXsd);

The xsdStream is null, so I guess the resource can't be found! But why?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

游魂 2024-09-13 07:12:43

资源的名称始终为:

..

因此,如果您的资源位于“Resources/Xsd/”中,并且您的默认项目命名空间为“MonitoringAPI.Configuration”,资源名称为:

“MonitoringAPI.Configuration.Resources.Xsd.MonitoringConfiguration.xsd”

另外,请确保资源的构建操作设置为“嵌入式资源”

The name of the resource is always:

<Base namespace>.<RelativePathInProject>.<FileName>

So if your resource is located in "Resources/Xsd/", and your default project namespace is "MonitoringAPI.Configuration", the resource name is:

"MonitoringAPI.Configuration.Resources.Xsd.MonitoringConfiguration.xsd"

Also make sure the build action for your resource is set to "Embedded Resource"

春庭雪 2024-09-13 07:12:43

获取嵌入资源的实际名称的简单而正确的方法:

string[] resourceNames =
    Assembly.GetExecutingAssembly().GetManifestResourceNames();

然后只需检查 resourceNames 数组,您就会知道要传递给 GetManifestResourceStream 方法的内容。

Easy and correct way to get the actual name of your embedded resource:

string[] resourceNames =
    Assembly.GetExecutingAssembly().GetManifestResourceNames();

Then simply check resourceNames array, and you will know for sure what to pass to GetManifestResourceStream method.

喜爱纠缠 2024-09-13 07:12:43

就我而言,

当您尝试通过 GetManifestResourceStream() 访问文件时。由于文件路径无效,您将收到错误,并且流将为空。

解决方案:

右键单击已添加到解决方案中的文件,然后单击“属性”。

选择构建操作作为嵌入式资源。 (而不是 Content - 默认情况下)

构建操作属性设置为嵌入资源

In my case,

When you try to access the file via GetManifestResourceStream(). You will get an error due to invalid path of the file, and stream will be null.

Solution:

Right click on the file which you have added in to solution and Click on Properties.

Select the Build Action as Embedded Resource. (Instead of Content - by default)

Build action property set to embedded resource

夏雨凉 2024-09-13 07:12:43

默认情况下,Visual Studio 不嵌入 xsd 文件,因此您必须确保 xsd 文件的“构建操作”属性设置为“嵌入资源”才能使其正常工作

By default, visual studio does not embed xsd file therefore you must ensure "Build Action" property of xsd file is set to "Embedded Resource" to make it works

栀子花开つ 2024-09-13 07:12:43

只需在 form1.resx 下添加您的资源 --> 添加现有项目,

双击您在资源文件夹下添加的资源。转到属性并选择“嵌入式资源”而不是“无”。

然后
尝试调试该行:

string[] resourceNames=Assembly.GetExecutingAssembly().GetManifestResourceNames();

检查您添加的资源是否在数组中。然后从该数组中准确复制资源名称并尝试将名称放入您的代码中..它工作正常!

just add your resources under form1.resx -->add existing items

double click on the resources you added under Resources folder.go to properties and select "Embedded Resources" instead of none.

Then
try debugging the line:

string[] resourceNames=Assembly.GetExecutingAssembly().GetManifestResourceNames();

check the resources you added are in the array. then copy the resource name exactly from this array and try putting the name on your code..it works fine!!

以往的大感动 2024-09-13 07:12:43

您可以通过传递资源名称来获取资源流,如下所示...

  1. 获取资源名称,例如。

    装配 objAssembly = Assembly.GetExecutingAssembly();

    string[] strResourceNames = objAssembly.GetManifestResourceNames();

  2. 将资源名称传递给...

    Stream strm = objAssembly.GetManifestResourceStream(strResourceNames);

现在您有了 Stream,您可以做任何您想做的事...

You can get the Resource Stream by passing the Resource Names which is as follows below...

  1. Get the resource name e.g..

    Assembly objAssembly = Assembly.GetExecutingAssembly();

    string[] strResourceNames = objAssembly.GetManifestResourceNames();

  2. Pass the Resource Names to ...

    Stream strm = objAssembly.GetManifestResourceStream(strResourceNames);

Now you have Stream you can do whatever you want...

美胚控场 2024-09-13 07:12:43

就我而言,情况完全不同:

我的 UWP 应用程序在调试和发布配置中正确编译,但 GetManifestResourceStream 仅返回 Null 发布配置。

问题是,在 UWP 构建配置文件中(并且仅在该文件中)启用了设置“使用 .NET Native 工具链编译”。禁用后,GetManifestResourceStream 按预期工作。

In my case, it was something completely different:

My UWP App compiled correctly in Debug and Release configuration but GetManifestResourceStream returned Null only Release configuration.

The issue was, that in the UWP Build Configuration file (and only there) the setting "Compile with .NET Native tool chain" was enabled. After disabling, GetManifestResourceStream worked as expected.

沉默的熊 2024-09-13 07:12:43

我遇到了一个问题,我将一整堆 .xsd 文件嵌入到许多不同的程序集中;一切正常(GetManifestResourceNames 正在返回我期望看到的文件),除了一个。不是的那个被称为:

Something.LA.xsd

我没有处理特定的文化,并且文件名末尾的 .LA 位被编译器拾取,因为该文件适用于 LA 文化 - 清单中的文件名是作为 Something.xsd (在文化 LA 下) - 因此我无法找到它(它最终出现在卫星程序集中)。我通过重命名文件来避免这个问题 - 大概可以明确地说明给定嵌入式资源的文化。

事实上,快速谷歌一下就会发现:
如何防止根据文件名设置嵌入式资源文件区域性

根据这个答案,你必须做一些 hacky 的事情 - 所以也许重命名文件并不是那么糟糕:)

I had an issue where I was embedding a whole heap of .xsd files in many different assemblies; everything was working (GetManifestResourceNames was returning the files I'd expect to see) except for one. The one which wasn't was called:

Something.LA.xsd

I wasn't dealing with specific cultures and the .LA bit at the end of the filename was being picked up by the compiler as this file being for the LA culture - the filename in the manifest was going in as Something.xsd (under culture LA) - hence me not being able to find it (it ended up in a satellite assembly). I dodged the issue by renaming the file - presumably it is possible to explicitly state the culture of a given embedded resource.

Actually, a quick google reveals:
How can I prevent embedded resource file culture being set based on its filename

According to this answer, you've got to do hacky things - so perhaps renaming the file wasn't so bad after all :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文