This is an add-in for Fody
Simplifies reading embedded resources from an Assembly.
Static resource names are checked at compile time. Use Resource.AsString
and Resource.AsStream
.
Runtime resource names are not check but can still make use of the helper code. Use Resource.AsStringUnChecked
and Resource.AsStreamUnChecked
.
https://nuget.org/packages/Resourcer.Fody/
PM> Install-Package Resourcer.Fody
Assuming you have an embedded resource at the root of your assembly named ResourceName
and your assembly is named AssemblyName
.
class Sample
{
void ReadResourceAsString()
{
var stringValue = Resource.AsString("ResourceName");
}
void ReadResourceAsStream()
{
var streamValue = Resource.AsStream("ResourceName");
}
}
class Sample
{
void ReadResourceAsString()
{
string stringValue;
var assembly = Assembly.GetExecutingAssembly();
using (var stream = assembly.GetManifestResourceStream("AssemblyName.ResourceName"))
using (var streamReader = new StreamReader(stream))
{
stringValue = streamReader.ReadToEnd();
}
}
void ReadResourceAsStream()
{
var assembly = Assembly.GetExecutingAssembly();
var streamValue = assembly.GetManifestResourceStream("AssemblyName.ResourceName");
}
}
Box designed by Mourad Mokrane from The Noun Project