EmbedResourceCSharp 1.1.3

Install-Package EmbedResourceCSharp -Version 1.1.3
dotnet add package EmbedResourceCSharp --version 1.1.3
<PackageReference Include="EmbedResourceCSharp" Version="1.1.3">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EmbedResourceCSharp --version 1.1.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: EmbedResourceCSharp, 1.1.3"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install EmbedResourceCSharp as a Cake Addin
#addin nuget:?package=EmbedResourceCSharp&version=1.1.3

// Install EmbedResourceCSharp as a Cake Tool
#tool nuget:?package=EmbedResourceCSharp&version=1.1.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.3 132 12/18/2021
1.1.2 88 12/17/2021
1.1.1 99 12/16/2021
1.1.0 95 12/15/2021