Posts Tagged Example

Embedded Resources in C#

So last night I was trying to think of the best way to compile images into the WWWinamp EXE.

One way I had considered was converting the images into a byte[] and then just putting this into the source code. The images I was working with were only about 200 bytes long, so this could be do-able.

Upon further investigation I came across this tutorial over at DevHood.com. It seemed a better idea to just roll with Embedded Resources in the EXE, since it would give me a few more options in the long run.

Below is the code I used to retrieve an embedded resource. You pass in the file name of the resource you want, and it'll return a byte[] with the content:

C#:
  1. public byte[] Server_ReadEmbeddedResource(string sFileName)
  2. {
  3. Assembly asResources = Assembly.GetExecutingAssembly(); //Referrence to the current assembly
  4. string[] sResNames = asResources.GetManifestResourceNames(); //Store list of resources for the current assembly in an array
  5.  
  6. foreach (string sResourceName in sResNames)
  7. {
  8. if(sResourceName.EndsWith(sFileName))
  9. {
  10. BinaryReader oBR = new BinaryReader(asResources.GetManifestResourceStream(sResourceName));
  11. return oBR.ReadBytes((int)asResources.GetManifestResourceStream(sResourceName).Length);
  12. }
  13. }
  14. return new byte[] { 0 };
  15. }

, , ,

No Comments