Skip to content

Instantly share code, notes, and snippets.

@Krizzzn
Created May 29, 2012 13:26
Show Gist options
  • Save Krizzzn/2828373 to your computer and use it in GitHub Desktop.
Save Krizzzn/2828373 to your computer and use it in GitHub Desktop.
method that loads the contents of a embedded ressource into a string.
private string LoadEmbeddedFile(string fileNameInAssembly, Encoding encoding)
{
var assembly = Assembly.GetExecutingAssembly();
var resName = from name in assembly.GetManifestResourceNames()
where name.ToLower().EndsWith(fileNameInAssembly.ToLower())
select name;
string text = default(string);
var fullResourceName = resName.FirstOrDefault();
if (fullResourceName == null)
throw new System.IO.FileNotFoundException(string.Format("Could not find file {0} in the resources of assembly {1}.", fileNameInAssembly, assembly.FullName));
using (System.IO.Stream stream = assembly.GetManifestResourceStream(fullResourceName)) {
Byte[] bytes = new Byte[stream.Length];
stream.Read(bytes, 0, (int)(stream.Length));
text = encoding.GetString(bytes);
}
return text;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment