Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pCYSl5EDgo/EmbeddingResourceCSharp
Embed resource files more C# programmer friendly!
https://github.com/pCYSl5EDgo/EmbeddingResourceCSharp
csharp csharp-sourcegenerator dotnet
Last synced: about 1 month ago
JSON representation
Embed resource files more C# programmer friendly!
- Host: GitHub
- URL: https://github.com/pCYSl5EDgo/EmbeddingResourceCSharp
- Owner: pCYSl5EDgo
- License: mit
- Created: 2020-11-07T08:11:40.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-06-04T13:23:36.000Z (6 months ago)
- Last Synced: 2024-08-01T22:43:39.293Z (4 months ago)
- Topics: csharp, csharp-sourcegenerator, dotnet
- Language: C#
- Homepage:
- Size: 89.8 KB
- Stars: 66
- Watchers: 3
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- RSCG_Examples - https://github.com/pCYSl5EDgo/EmbeddingResourceCSharp
README
# EmbedResourceCSharp
This is a [C# Source Generator](https://devblogs.microsoft.com/dotnet/new-c-source-generator-samples/).
This let you embed files in your application.
You do not need to use `Assembly.GetManifestResourceStream` anymore.# How to use
## Install
```sh
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```csharp
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.
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 GetFileContentA();
}
}
```You can get file content byte sequence with static partial method `System.ReadOnlySpan 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```csharp
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.
One parameter must exist and its type must be System.ReadOnlySpan. 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 GetResouceFileContent(System.ReadOnlySpan 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.