EmbedResourceCSharp by pCYSl5EDgo
Nuget / site data
Details
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
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
reading embedded resources fast
How to use
Example ( source csproj, source files )
- CSharp Project
- Program.cs
- MyResource.cs
- createDB.txt
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>
This is the use of EmbedResourceCSharp in Program.cs
// See https://aka.ms/new-console-template for more information
using System;
using System.Text;
var value = EmbeddingResourceCSharpDemo.MyResource.GetContentOfCreate();
StringBuilder sb = new ();
foreach (byte b in value)
{
sb.Append((char)b);
}
;
//EncodingExtensions.GetString(Encoding.UTF8, value);
Console.WriteLine(sb.ToString());
This is the use of EmbedResourceCSharp in MyResource.cs
namespace EmbeddingResourceCSharpDemo;
public partial class MyResource
{
[EmbedResourceCSharp.FileEmbed("createDB.txt")]
public static partial System.ReadOnlySpan<byte> GetContentOfCreate();
}
This is the use of EmbedResourceCSharp in createDB.txt
create database Andrei;
GO;
use Andrei;
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- Attribute.cs
- MyResource____GetContentOfCreate.file.g.cs
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;
}
}
}
namespace EmbeddingResourceCSharpDemo
{
public partial class MyResource
{
public static partial global::System.ReadOnlySpan<byte> GetContentOfCreate()
{
return new byte[] { 239, 187, 191, 99, 114, 101, 97, 116, 101, 32, 100, 97, 116, 97, 98, 97, 115, 101, 32, 65, 110, 100, 114, 101, 105, 59, 13, 10, 71, 79, 59, 13, 10, 117, 115, 101, 32, 65, 110, 100, 114, 101, 105, 59 };
}
}
}
Usefull
Download Example (.NET C# )
Share EmbedResourceCSharp
https://ignatandrei.github.io/RSCG_Examples/v2/docs/EmbedResourceCSharp