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");
Method 1: Add existing file, set property to Embedded Resource. Add the file to your project, then set the type to Embedded Resource . NOTE: If you add the file using this method, you can use GetManifestResourceStream to access it (see answer from @dtb).
Embedded resource has no predefined structure: it is just a named blob of bytes. So called “. resx” file is a special kind of embedded resource. It is a string -to-object dictionary that maps a name to an object. The object may be a string , an image, an icon, or a blob of bytes.
Open Solution Explorer add files you want to embed. Right click on the files then click on Properties . In Properties window and change Build Action to Embedded Resource . After that you should write the embedded resources to file in order to be able to run it.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With