EmbeddingResourceCSharp
Embed resource files more C# programmer friendly!
Install / Use
/learn @pCYSl5EDgo/EmbeddingResourceCSharpREADME
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.
Related Skills
node-connect
345.4kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
104.6kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
345.4kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
345.4kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
