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

In the same category (FilesToCode) - 13 other generators

Chorn.EmbeddedResourceAccessGenerator

corecraft

Datacute.EmbeddedResourcePropertyGenerator

DotnetYang

LingoGen

NotNotAppSettings

Podimo.ConstEmbed

ResXGenerator

RSCG_JSON2Class

RSCG_Utils

ThisAssembly_Resources

ThisAssembly.Strings

Weave