Skip to content

Source generator to easily embed and access files + directory trees

License

Notifications You must be signed in to change notification settings

chsienki/AutoEmbed

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AutoEmbed

Automatically add files and directory structures to your assembly, with easy generated access at runtime.

Usage

Reference the generator, and add any files you want to embed to your project file via the AutoEmbed item.

<ItemGroup>
    <AutoEmbed Include="files\**" />
</ItemGroup>

Usual MSBuild rules apply, so you may use wildcards, excludes etc.

The generate will add class called Resources to your compilation under the namespace AutoEmbed which contains properties allowing you to access the embedded resources at runtime.

    string fileA = Resources.fileA_txt;

Files within subdirectories are included within sub-types:

    string fileB = Resources.subdir.fileB_txt;

Note: nested directories with a single child directory are 'collapsed' into the parent folder.

Files are not limited to text only. The underlying type used to represent the resource contains implicit conversion for string, byte[] and stream meaning you can read a binary file either directly as an array or pass to a reader as a stream.

byte[] fileC = Resources.subdir.fileC_bin;

using (var fileCStream = new BinaryReader(Resources.subdir.fileC_bin))
{
    Console.WriteLine(fileCStream.ReadInt32());
    Console.WriteLine(fileCStream.ReadBoolean());
    Console.WriteLine(fileCStream.ReadString());
}

The underlying objects also define ReadAsString(), ReadAsByteArray() and ReadAsStream() to allow you to access the data without relying on conversions

var s = Resources.fileA_txt.ReadAsString();

About

Source generator to easily embed and access files + directory trees

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages