简体   繁体   English

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

[英]Can't load a manifest resource with GetManifestResourceStream()

I've created a custom configuration section using XSD. 我已经使用XSD创建了自定义配置部分。 In order to parse the config file that follows this new schema, I load the resource (my .xsd file) with this: 为了解析这个新模式之后的配置文件,我加载了资源(我的.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);
        }

    }

By the way my resource is: MonitoringConfiguration.xsd. 顺便说一下,我的资源是:MonitoringConfiguration.xsd。 And the namespace of the other partial class (that represents the code behind of the .xsd file) is MonitoringAPI.Configuration . 而另一个分类的名称空间(代表.xsd文件后面的代码)是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! xsdStream为null,所以我猜想无法找到资源! But why? 但为什么?

Thank you 谢谢

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: 因此,如果您的资源位于“Resources / Xsd /”中,并且您的默认项目命名空间是“MonitoringAPI.Configuration”,则资源名称为:

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

Also make sure the build action for your resource is set to "Embedded Resource" 还要确保资源的构建操作设置为“嵌入式资源”

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. 然后只需检查resourceNames数组,您就会知道要传递给GetManifestResourceStream方法的内容。

In my case, 就我而言,

When you try to access the file via GetManifestResourceStream() . 当您尝试通过GetManifestResourceStream()访问该文件时。 You will get an error due to invalid path of the file, and stream will be null. 由于文件路径无效,您将收到错误,并且流将为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 . 选择Build Action as Embedded Resource (Instead of Content - by default) (而不是Content - 默认情况下)

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

默认情况下,visual studio不会嵌入xsd文件,因此必须确保将xsd文件的“Build Action”属性设置为“Embedded Resource”才能使其正常工作

just add your resources under form1.resx -->add existing items 只需在form1.resx下添加资源 - >添加现有项目

double click on the resources you added under Resources folder.go to properties and select "Embedded Resources" instead of none. 双击您在Resources folder.go下添加到资源的资源,然后选择“Embedded Resources”而不是“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!! 然后从这个数组中精确复制资源名称,并尝试将名称放在您的代码上..工作正常!

You can get the Resource Stream by passing the Resource Names which is as follows below... 您可以通过传递资源名称来获取资源流,如下所示......

  1. Get the resource name eg. 获取资源名称,例如。

    Assembly objAssembly = Assembly.GetExecutingAssembly(); 程序集objAssembly = Assembly.GetExecutingAssembly();

    string[] strResourceNames = objAssembly.GetManifestResourceNames(); string [] strResourceNames = objAssembly.GetManifestResourceNames();

  2. Pass the Resource Names to ... 将资源名称传递给...

    Stream strm = objAssembly.GetManifestResourceStream(strResourceNames); Stream strm = objAssembly.GetManifestResourceStream(strResourceNames);

Now you have Stream you can do whatever you want... 现在你有Stream你可以随心所欲......

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. 我的UWP App在Debug和Release配置中正确编译,但GetManifestResourceStream仅返回Null Release配置。

The issue was, that in the UWP Build Configuration file (and only there) the setting "Compile with .NET Native tool chain" was enabled. 问题是,在UWP构建配置文件中(并且仅在那里)启用了“使用.NET Native工具链编译 ”设置。 After disabling, GetManifestResourceStream worked as expected. 禁用后,GetManifestResourceStream按预期工作。

I had an issue where I was embedding a whole heap of .xsd files in many different assemblies; 我有一个问题,我在许多不同的程序集中嵌入了一大堆.xsd文件; everything was working (GetManifestResourceNames was returning the files I'd expect to see) except for one. 一切正常(GetManifestResourceNames正在返回我希望看到的文件),除了一个。 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). 我没有处理特定的文化,文件名末尾的.LA位被编译器拾取,因为这个文件是针对LA文化的 - 清单中的文件名是以Something.xsd(在文化下)洛杉矶) - 因此我无法找到它(它最终在卫星装配中)。 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 :) 根据这个答案,你必须做hacky事情 - 所以也许重命名文件毕竟不是那么糟糕:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM