Created
May 29, 2012 13:26
-
-
Save Krizzzn/2828373 to your computer and use it in GitHub Desktop.
method that loads the contents of a embedded ressource into a string.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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