Skip to content

Instantly share code, notes, and snippets.

@kristopherjohnson
Created August 1, 2012 17:53
Show Gist options
  • Save kristopherjohnson/3229248 to your computer and use it in GitHub Desktop.
Save kristopherjohnson/3229248 to your computer and use it in GitHub Desktop.
C# snippet: Read embedded resource file
/// <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
Copy link

What is Filename ?My Program fails :(

@mikiqex
Copy link

mikiqex commented Jan 9, 2019

@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