Code Example: Embedded resource extraction
If .Net project contains embedded resources, we can extract them at run-time (for internal-in-program usage or to save as files). Embedded resource can be attached file of any type (popular types: texts, images, sounds).
For example, .Net project contains embedded "License Agreement" text file:
File properties:

This code shows how to load embedded text file into string "license":
public void ExtractResources()
{
Stream sin = Assembly.GetExecutingAssembly().GetManifestResourceStream("SegevSystems.Setup.Files.LICENSE AGREEMENT.TXT");
StreamReader r = new StreamReader(sin);
string license = r.ReadToEnd();
r.Close();
r.Dispose();
sin.Close();
sin.Dispose();
}
// "SegevSystems.Setup.Files" represents namespace where resources are located
This code shows how to save all embedded resources into files:
public void ExtractResources()
{
try
{
Assembly a = Assembly.GetExecutingAssembly();
string[] contents = a.GetManifestResourceNames();
for (int i = 0; i < contents.Length; i++)
{
using (Stream s = a.GetManifestResourceStream(contents[i]))
{
using (BufferedStream b = new BufferedStream(s))
{
string filePath = string.Format("c:\\temp\\{0}", contents[i].Replace("SegevSystems.Setup.Files.", string.Empty));
if (File.Exists(filePath)) File.Delete(filePath);
using (FileStream f = new FileStream(filePath, FileMode.CreateNew))
{
int _res;
while ((_res = b.ReadByte()) >= 0)
{
f.WriteByte((byte) _res);
}
f.Close();
}
b.Close();
}
s.Close();
}
}
}
catch (Exception ex)
{
Debug.WriteLine(string.Format("Error while resource read/write: {0}", ex.Message));
}
}
Hope that this post was helpful :)