Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/chsienki/AutoEmbed
Source generator to easily embed and access files + directory trees
https://github.com/chsienki/AutoEmbed
Last synced: about 1 month ago
JSON representation
Source generator to easily embed and access files + directory trees
- Host: GitHub
- URL: https://github.com/chsienki/AutoEmbed
- Owner: chsienki
- License: mit
- Created: 2021-02-22T18:50:17.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2021-02-22T19:06:32.000Z (almost 4 years ago)
- Last Synced: 2024-08-01T22:43:50.506Z (4 months ago)
- Language: C#
- Size: 9.77 KB
- Stars: 17
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- RSCG_Examples - AutoEmbed
README
# 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.
```xml
```
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.
```csharp
string fileA = Resources.fileA_txt;
```Files within subdirectories are included within sub-types:
```csharp
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.
```csharp
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
```csharp
var s = Resources.fileA_txt.ReadAsString();
```