Skip to main content

EmbedResourceCSharp by pCYSl5EDgo

NuGet / site data​

Nuget GitHub last commit GitHub Repo stars

Details​

Info​

info

Name: EmbedResourceCSharp

SourceGenerator for resource file embedding with EmbedResourceCSharp.

Author: pCYSl5EDgo

NuGet: https://www.nuget.org/packages/EmbedResourceCSharp/

You can find more details at https://github.com/pCYSl5EDgo/EmbeddingResourceCSharp

Source: https://github.com/pCYSl5EDgo/EmbeddingResourceCSharp

Original Readme​

note

EmbedResourceCSharp

This is a C# Source Generator. This let you embed files in your application. You do not need to use Assembly.GetManifestResourceStream anymore.

How to use

Install​

dotnet add package EmbedResourceCSharp

Add only 1 package to your C# project.

Embedding file​

Provide that there are some files like below.

  • projectFolder/
    • Example.csproj
    • ExampleProgram.cs
    • resourceFileA.txt
namespace Example
{
// partial methods require partial class/struct!
public partial class ExampleClass
{
/*
The relative file path from C# project folder should be specified.
The return value type must be System.ReadOnlySpan<byte>.
No parameter must exist.
The method must be static and partial.
The accessibility of the method does not matter.
*/
[EmbedResourceCSharp.FileEmbed("resourceFileA.txt")]
private static partial System.ReadOnlySpan<byte> GetFileContentA();
}
}

You can get file content byte sequence with static partial method System.ReadOnlySpan<byte> GetFileContentA.

Embedding files under specific folder​

Provide that there are some files like below.

  • projectFolder/
    • Example2.csproj
    • ExampleProgram.cs
  • folderB/
    • resourceA.txt
    • resourceB.txt
    • folderB_C/
      • resourceC.txt
    • resourceD.csv
namespace Example2
{
// partial methods require partial class/struct!
public partial class ExampleClass
{
/*
The relative folder path from C# project folder should be specified. The folder path should end with slash or backslash.
The return value type must be System.ReadOnlySpan<byte>.
One parameter must exist and its type must be System.ReadOnlySpan<char>. The parameter name does not matter.
The method must be static and partial.
The accessibility of the method does not matter.
*/
[EmbedResourceCSharp.FolderEmbed("../folderB/", "*.txt")]
private static partial System.ReadOnlySpan<byte> GetResouceFileContent(System.ReadOnlySpan<char> path);

public static void Main()
{
// Specify relative path from the folder.
var aContent = GetResouceFileContent("resourceA.txt");
var bContent = GetResouceFileContent("resourceB.txt");
var cContent = GetResouceFileContent("folderB_C/resourceC.txt");
// var dContent = GetResouceFileContent("resourceD.csv");
// Above method call throws an FileNotFoundException!
}
}
}

You can include all files under the target folder recursively. You can filter file with search pattern.

About​

note

reading embedded resources fast

How to use​

Example (source csproj, source files)​

This is the CSharp Project that references EmbedResourceCSharp

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<ItemGroup>
<None Remove="createDB.txt" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="createDB.txt" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="EmbedResourceCSharp" Version="1.1.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>

Generated Files​

Those are taken from $(BaseIntermediateOutputPath)\GX

namespace EmbedResourceCSharp
{
internal enum PathSeparator
{
AsIs,
Slash,
BackSlash,
}

[global::System.AttributeUsage(global::System.AttributeTargets.Method, AllowMultiple = false)]
internal sealed class FileEmbedAttribute : global::System.Attribute
{
public string Path { get; }

public FileEmbedAttribute(string path)
{
Path = path;
}
}

[global::System.AttributeUsage(global::System.AttributeTargets.Method, AllowMultiple = false)]
internal sealed class FolderEmbedAttribute : global::System.Attribute
{
public string Path { get; private set; }
public string Filter { get; private set; }
public global::System.IO.SearchOption Option { get; private set; }
public PathSeparator Separator { get; private set; }

public FolderEmbedAttribute(string path, string filter = "*", global::System.IO.SearchOption option = global::System.IO.SearchOption.AllDirectories, PathSeparator separator = PathSeparator.Slash)
{
Path = path;
Filter = filter;
Option = option;
Separator = separator;
}
}
}

Useful​

Download Example (.NET C#)​

Share EmbedResourceCSharp​

https://ignatandrei.github.io/RSCG_Examples/v2/docs/EmbedResourceCSharp

aaa

Category "FilesToCode" has the following generators:​

1 Chorn.EmbeddedResourceAccessGenerator Nuget GitHub Repo stars 2024-01-21

2 corecraft Nuget GitHub Repo stars 2024-02-17

3 Datacute.EmbeddedResourcePropertyGenerator Nuget GitHub Repo stars 2024-11-03

4 DotnetYang Nuget GitHub Repo stars 2024-06-29

5 EmbedResourceCSharp Nuget GitHub Repo stars 2023-04-16

6 kli.Localize Nuget GitHub Repo stars 2025-10-01

7 LingoGen Nuget GitHub Repo stars 2024-02-23

8 Maestria.TypeProviders Nuget GitHub Repo stars 2026-04-09

9 MagicConstants Nuget GitHub Repo stars 2026-09-04

10 NFH.FileEmbed Nuget GitHub Repo stars 2025-08-08

11 NotNotAppSettings Nuget GitHub Repo stars 2024-01-26

12 Pinecone.TypedPath Nuget GitHub Repo stars 2026-08-24

13 Podimo.ConstEmbed Nuget GitHub Repo stars 2023-04-16

14 ResXGenerator Nuget GitHub Repo stars 2023-10-02

15 RSCG_JSON2Class Nuget GitHub Repo stars 2024-02-29

16 RSCG_Utils Nuget GitHub Repo stars 2023-04-16

17 Slang.Net Nuget GitHub Repo stars 2026-09-11

18 Strings.ResourceGenerator Nuget GitHub Repo stars 2025-07-06

19 SvgIconGenerator Nuget GitHub Repo stars 2026-04-04

20 ThisAssembly_Resources Nuget GitHub Repo stars 2023-09-16

21 ThisAssembly.Strings Nuget GitHub Repo stars 2024-07-21

22 TypedPaths Nuget GitHub Repo stars 2026-04-01

23 Weave Nuget GitHub Repo stars 2024-01-27

See category​

FilesToCode