Skip to content

Instantly share code, notes, and snippets.

@wocar
Created January 10, 2020 19:28
Show Gist options
  • Select an option

  • Save wocar/1701696c54f3498f55685d091eefb0f1 to your computer and use it in GitHub Desktop.

Select an option

Save wocar/1701696c54f3498f55685d091eefb0f1 to your computer and use it in GitHub Desktop.
Read embedded resource from assembly
public static class TestExtensions
{
public static string ReadResource(this Assembly assembly, string name)
{
// Determine path
var resourcePath = assembly.GetManifestResourceNames()
.Single(str => str.EndsWith(name));;
using (var stream = assembly.GetManifestResourceStream(resourcePath))
using (var reader = new StreamReader(stream ?? throw new InvalidOperationException()))
{
return reader.ReadToEnd();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment