If I include files in my Silverlight app and need to load them as raw byte arrays programatically, which Build Action should I set them as, and how do I refer to them? If they are included as Content, can I load them by path as a regular file? If I include them as Resource is there a collection of embedded resources where I can grab a reference to them? What are the pros and cons and differences between the two approaches?
views:
39answers:
1
+3
A:
Set the Build Action to "Embedded Resource" and use Assembly.GetManifestResourceStream
to load them. I believe the "Resource" build action is meant for resource files (resx) which are slightly different, although I've never been entirely sure :)
If you include them as regular content, you may have difficulty deploying the files - I'm not entirely sure, to be honest. (That's a Silverlight-specific area.)
Personally I like using Embedded Resources so long as you don't need to be able to update them independently - at that point it's helpful to have them on disk as normal files.
Jon Skeet
2010-08-30 18:40:45
Admit it Jon, you're actually just a script one of the SO devs wrote that uses an algorythm to identify questions which fit a boilerplate answer you have programmed in. Devious.
Jimmy Hoffa
2010-08-30 18:47:23
@Jimmy: LOL. In fact I've just come back from holiday... probably just going to go for one accepted answer today, and then play Worms Reloaded for the rest of the night :)
Jon Skeet
2010-08-30 18:51:55
I set my files to Embedded Resource, and I call GetManifestResourceStream on the executing assembly with the relative path to my file as in the examples I've seen online (with no leading slash), and I'm getting a null object back. Any ideas?
Rich
2010-08-30 18:54:07
@Jon Skeet: and you call that 'back from holiday' ? What are you, in one of them countries that doesn't work people to death?
Jimmy Hoffa
2010-08-30 19:07:20
@Rich: Chances are you're not providing the right path, or possibly not using the right assembly. If you can produce a short but complete program which demonstrates the problem, I can look into it...
Jon Skeet
2010-08-30 19:23:55
"Silverlight embedded resources must always use the Resource build action, and not the Embedded Resource build action, which uses a format that Silverlight cannot recognize."http://msdn.microsoft.com/en-us/library/cc296240(VS.95).aspx
Dan Auclair
2010-08-30 19:56:29