5

I added an XML file as an embedded resource in my class library by using the accessing the project properties in Visual Studio and then Resources | Add Resource | Add Existing File...

I've tried to access the file using the following code, but I keep getting a null reference returned. Anyone have any ideas?

var path = Server.MapPath("~/bin/MyAssembly.dll");
var assembly = Assembly.LoadFile(path);
var stream = assembly.GetManifestResourceStream("MyNamespace.filename.xml");
starblue
  • 55,348
  • 14
  • 97
  • 151
Kevin Babcock
  • 10,187
  • 19
  • 69
  • 89

3 Answers3

6

I find it much easier to use the "Resources" tab of the project's properties dialog in Visual Studio. Then you have a generated strongly typed reference to your resource through:

Properties.Resources.Filename
Shawn Miller
  • 7,082
  • 6
  • 46
  • 54
3

The MSDN page for GetManifestResourceStream makes this note:

This method returns a null reference (Nothing in Visual Basic) if a private resource in another assembly is accessed and the caller does not have ReflectionPermission with the ReflectionPermissionFlag.MemberAccess flag.

Have you marked the resource as "public" in your assembly?

Matt Hamilton
  • 200,371
  • 61
  • 386
  • 320
  • I have indeed marked it as public in the assembly. Still a null reference. – Kevin Babcock Feb 06 '09 at 03:13
  • Perhaps it's something to do with the "Build Action" setting for that file. Have you tried changing it to different values (eg Embedded Resource vs Content)? – Matt Hamilton Feb 06 '09 at 03:15
  • I never set the build action for the file. I only imported the file into a .resx file and was having trouble extracting its contents. Getting rid of the .resx and marking the Build Action as "Embedded Resource" allowed me to access it using the code above. Thanks very much for the tip! – Kevin Babcock Feb 06 '09 at 03:22
0

I found that this article explains nicely how to set this up: http://www.devx.com/dotnet/Article/10831/1954

One thing that may have helped in this case is using a different method for determining the location of the embedded resource.

  • GetEntryAssembly: Use this method to reference the executable file that was run to start the current process. The entry assembly is set for Windows forms applications, but for most other applications (for example, web projects), GetEntryAssembly returns 'nothing' (or null, in C#).
  • GetExecutingAssembly: Use this method to reference the same assembly that the executing code is in.
  • GetCallingAssembly: Use this method to reference the assembly containing the code that called the current method. This method is useful if you write generic code to read embedded resources that is inside a shared assembly where the embedded resource is in the calling assembly.

I personally prefer this method over using the "Resources" tab because it allows me to use other tools to add resources to the project for inclusion on compilation. I don't have to use the GUI with this tool

cjbarth
  • 4,189
  • 6
  • 43
  • 62