61

I have a web application project. I generated the DLL and import it in another project. I implemented VirtualPathProvider.

I followed this web site: http://support.microsoft.com/kb/910441/en-us?spid=8940&sid=global, and everything works until I add another site master.

  1. I added site_export.master and changed its Build Action to Embedded Resource.
  2. I changed my page to use the new site master.
  3. GetManifestResourceStream() returns null when I load site_export.master.
  4. I call GetManifestResourceNames() to check if site_export.master exists in the DLL and it does. It's in the list. All of the name spaces match. I didn't list the name space out here.

Why can't GetManifestResourceStream() load my new site_export.master? It loads site.master just fine. I know my DLL is loaded because I can see other files in the DLL.

leppie
  • 115,091
  • 17
  • 196
  • 297
MsBugKiller
  • 813
  • 1
  • 7
  • 12
  • 2
    possible duplicate of [Can't load a manifest resource with GetManifestResourceStream()](http://stackoverflow.com/questions/3068736/cant-load-a-manifest-resource-with-getmanifestresourcestream) – nawfal Feb 13 '14 at 10:32
  • Once I get one working I tend to cut, paste and edit it to create more – Kirsten Jul 27 '19 at 00:56

5 Answers5

107

Remember the following issues...

Step 1. Build action set to embedded resource see

C#’s GetManifestResourceStream Gotcha

Step 2. Use namespace.resourcename see GetManifestResourceStream() returns null ?

Actually, this method returns null if a private resource in another assembly is accessed and the caller does not have ReflectionPermission with the ReflectionPermissionFlag.MemberAccess flag.

RajeshKdev
  • 6,365
  • 6
  • 58
  • 80
Shahdat
  • 5,343
  • 4
  • 37
  • 37
  • 4
    Haha... Embedded resource... Thank you very much, saved a lot of time :) – Chuck Norris Dec 06 '13 at 20:24
  • 5
    To mention that in the project properties page the Default Namespace is defined. This is the namespace to use. – Diego Frehner Jul 29 '14 at 14:19
  • 3
    If the image is in a sub-folder, use `namespace.subfolder.resourcename`. – avenmore Nov 17 '15 at 15:01
  • 1
    This post explains how to do Step 1 of the above solution (set Build Action to 'Embedded resource') https://stackoverflow.com/questions/10587875/change-build-action-to-embedded-resource-in-c-sharp – uncleGe Aug 06 '17 at 20:36
94

Side-hint. To make sure you're in the right assembly and with right name: dump and evaluate all the resources available in your target assembly

string[] names = assembly.GetManifestResourceNames();

(in my case, I misused a namespace from another assembly)

Yury Schkatula
  • 5,291
  • 2
  • 18
  • 42
  • 10
    In simple case paste this to "Immediate Window" while debugging: `Assembly.GetExecutingAssembly().GetManifestResourceNames();` – Juha Palomäki Feb 24 '18 at 20:45
  • @JuhaPalomäki, it may help, but not in all cases (so you would receive something like "evaluation requires all threads to run"), especially in VS2017. – Yury Schkatula Feb 25 '18 at 11:02
  • 1
    Great when you are at the exception and run it at debug :) – DoomVroom Oct 07 '19 at 18:50
  • If it doesn't work in the immediate window you can just inspect a variable that you set in the code: var dudes = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames(); – Jack Sep 06 '20 at 22:54
  • Thank You. The issue was wrong path to the resource. – SkyDancer Dec 10 '21 at 22:55
14

I did this to make it work:

Step 1: Select the resource (CRDF.xsl in my case) and right click > Properties. Set Build Action to "EmbeddedResource" and Logical Name to name of your choice, e.g. CRDFXSL.

Step 2 : Change your Source code like this:

Assembly _assembly;
  _assembly = Assembly.GetExecutingAssembly();         
  xslStream = _assembly.GetManifestResourceStream("CRDFXSL");
  _xmlReader = XmlReader.Create(xslStream);

Thus everything went smoooooooth..

  • One thing to add - you may need to set `` in *.csproj file [manually](https://social.msdn.microsoft.com/Forums/vstudio/en-US/c94da8fd-0b0f-45c0-b137-25ab5ccc7e53/create-project-item-with-the-logicalnamelogicalname-attribute-in-the-project-file?forum=vsx). – Anton Krouglov Jul 04 '17 at 11:20
4

Hint and Caution: If the "Assembly name" and "Default namespace" does not match in the project file then also GetManifestResourceStream would return null. GetManifestResourceNames still returns the names of assemblies but during loading the manifest would not work.

ShuttlePod
  • 56
  • 4
0

Try this:

Dim ctx As Windows.ApplicationModel.Resources.Core.ResourceContext = New Windows.ApplicationModel.Resources.Core.ResourceContext()
ctx.Languages = {Globalization.CultureInfo.CurrentUICulture.Name}
Dim rmap As Windows.ApplicationModel.Resources.Core.ResourceMap = Windows.ApplicationModel.Resources.Core.ResourceManager.Current.MainResourceMap
Dim res = rmap.GetValue("Assets/sample.png", ctx)
Dim resFile = Await res.GetValueAsFileAsync

The Windows.ApplicationModel.Resources.Core.ResourceManager.Current.MainResourceMap list all resources.

user247702
  • 23,641
  • 15
  • 110
  • 157
Rtisatto
  • 767
  • 8
  • 7