GetManifestResourceStream returns NULL

送分小仙女□ 提交于 2019-11-26 19:26:53

问题


This is a C# .NET 4.0 application:

I'm embedding a text file as a resource and then trying to display it in a dialog box:

    var assembly = Assembly.GetExecutingAssembly();
    var resourceName = "MyProj.Help.txt";

        using (Stream stream = assembly.GetManifestResourceStream(resourceName))
        {
            using (StreamReader reader = new StreamReader(stream))
            {
                string result = reader.ReadToEnd();
                System.Windows.Forms.MessageBox.Show(result, "MyProj", MessageBoxButtons.OK);
            }
        }

The solution is MyProjSolution and the executable is MyProj.exe. Help.txt is an embedded resource. However, the stream is null. I've tried MyProjSolution.Help.txt and MyProjSolution.MyProj.Help.txt but nothing seems to work.


回答1:


You can check that the resources are correctly embedded by using

//From the assembly where this code lives!
this.GetType().Assembly.GetManifestResourceNames()

//or from the entry point to the application - there is a difference!
Assembly.GetExecutingAssembly().GetManifestResourceNames()

when debugging. This will list all the (fully qualified names) of all resources embedded in the assembly your code is written in.

See Assembly.GetManifestResourceNames() on MSDN.

Simply copy the relevant name, and use that instead of whatever you have defined in the variable 'resourceName'.

Notes - the resource name is case sensitive, and if you have incorrectly embedded the resource file, it will not show up in the list returned by the call to GetManifestResourceNames(). Also - make sure you are reading the resource from the correct assembly (if multiple assemblies are used) - it's all to easy to get the resources from the currently executing assembly rather than from a referenced assembly.

EDIT - .NET Core
Please see this SO post for details on how to embed using .NET Core.

Retrieving the manifest info looks to be similar - just use this.GetType().GetTypeInfo().Assembly.GetManifestResourceNames() to get the a manifest from the assembly where the code is executing.

I haven't figured out how to do the equivalent of Assembly.GetExecutingAssembly() in .NET Core yet! if anyone knows - please let me know and I will update this answer.




回答2:


I had a similar issue check first that the file is included in your project , then go to properties and set the build action of that file to Embedded Resource . this worked for me .




回答3:


Here is the cause of my null value.

http://adrianmejia.com/blog/2011/07/18/cs-getmanifestresourcestream-gotcha/

The GetManifestResourceStream method will always returns NULL if the resource ‘built action‘ property is not set to ‘embedded resource‘

After setting this property with all the needed files assembly.GetManifestResourceStream starts returning the correct stream instead of NULL.




回答4:


Just a warning.

I could not access my file as an embedded resource even though I specified that it was and even though it had that Build Action property. Wasted a lot of time banging my head. I embedded a csharp code file with .txt appended to its name (xxx.cs.txt). For some reason the GetManifestResourceNames() and GetManifestResourceStream() methods won't see a file with .cs in its name.

I renamed it simply xxx.txt and everything was fine.

Weird.




回答5:


The embedded file's "Build Action" property should be set as "Embedded Resource" to run the line, which is given below, properly:

Stream stream = assembly.GetManifestResourceStream(resourceName)

Right click on the file, click the property and then set "Build Action" property as "Embedded Resource":




回答6:


I had the same problem, thanks to Jay I found it was hyphens in the directory name.

ProjectName.ResourceFolder.Sub-Directory becomes ProjectName.ResourceFolder.Sub_Directory when you reference the resource stream.




回答7:


In my case the problem was that the code looking for the resource was in a different project that the resource itself.

You can only access resources that are in the same project the code is. I thought I could put all my resources in the web page project, but I need images in the mail project too.

Hope this helps someone in the same situation I was.

I find really useful calling Assembly.GetExecutingAssembly().GetManifestResourceNames();.




回答8:


    First Unload the project and click on edit the project file. 

    Inside the project file make sure that the item you are fetching from the assembly is included inside <EmbeddedResource> tag.

    Eg: 

         <ItemGroup>
          <EmbeddedResource Include="Template\ForExampleFile.html" />
         </ItemGroup>


    The files I added into the project were just in Content tag but not in the EmbeddedResource as shown below by default. Hence the stream was returning null.
    <ItemGroup>
        <Content Include="Template\ForExampleFile.html" />
  </ItemGroup>



回答9:


You need to unload your solution.Then edit project.Afterfind your folder and change like this:

<EmbeddedResource Include="yourpath" />



回答10:


In case it helps anyone else, Make sure Assembly.GetExecutingAssembly() line is called from same assembly which as embedded resources.




回答11:


You probably need to specify the path to your txt file in the GetManifestResourceStream parameter, or you could try sticking the txt file in the same directory as your executable. Hope that helps!



来源:https://stackoverflow.com/questions/21637830/getmanifestresourcestream-returns-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!