views:

39

answers:

1

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?

+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
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
@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
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
@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
@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
"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