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:

[csharp]

public byte[] Server_ReadEmbeddedResource(string sFileName)
{
Assembly asResources = Assembly.GetExecutingAssembly(); //Referrence to the current assembly
string[] sResNames = asResources.GetManifestResourceNames(); //Store list of resources for the current assembly in an array

foreach (string sResourceName in sResNames)
{
if(sResourceName.EndsWith(sFileName))
{
BinaryReader oBR = new BinaryReader(asResources.GetManifestResourceStream(sResourceName));
return oBR.ReadBytes((int)asResources.GetManifestResourceStream(sResourceName).Length);
}
}
return new byte[] { 0 };
}

[/csharp]

, , ,

  1. No comments yet.
(will not be published)