Created
August 1, 2012 17:53
-
-
Save kristopherjohnson/3229248 to your computer and use it in GitHub Desktop.
C# snippet: Read embedded resource file
This file contains 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
/// <summary> | |
/// Read contents of an embedded resource file | |
/// </summary> | |
private string ReadResourceFile(string filename) | |
{ | |
var thisAssembly = Assembly.GetExecutingAssembly(); | |
using (var stream = thisAssembly.GetManifestResourceStream(filename)) | |
{ | |
using (var reader = new StreamReader(stream)) | |
{ | |
return reader.ReadToEnd(); | |
} | |
} | |
} |
@saharslm: You have to include namespace. For example, if you embed a text file named "MyFile.txt"
that is placed in the root of a project with default namespace "MyCompany.MyProduct"
, then filename is "MyCompany.MyProduct.MyFile.txt"
.
You can get a list of all resources in an assembly using the Assembly.GetManifestResourceNames
method.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What is Filename ?My Program fails :(