GetManifestResourceStream returns NULL

前端 未结 13 2556
走了就别回头了
走了就别回头了 2020-11-28 05:41

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 = Assembl         


        
相关标签:
13条回答
  • 2020-11-28 05:50

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

    <EmbeddedResource Include="yourpath" />
    
    0 讨论(0)
  • 2020-11-28 05:52

    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.

    0 讨论(0)
  • 2020-11-28 05:54

    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.

    0 讨论(0)
  • 2020-11-28 05:54

    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!

    0 讨论(0)
  • 2020-11-28 06:00

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

    0 讨论(0)
  • 2020-11-28 06:00
        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>
    
    0 讨论(0)
提交回复
热议问题