RSCG – EmbedResourceCSharp
| name | EmbedResourceCSharp |
| nuget | https://www.nuget.org/packages/EmbedResourceCSharp/ |
| link | https://github.com/pCYSl5EDgo/EmbeddingResourceCSharp |
| author | pCYSl5EDgo |
reading embedded resources fast
This is how you can use EmbedResourceCSharp .
The code that you start with is
<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>
The code that you will use is
// 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());
namespace EmbeddingResourceCSharpDemo;
public partial class MyResource
{
[EmbedResourceCSharp.FileEmbed("createDB.txt")]
public static partial System.ReadOnlySpan<byte> GetContentOfCreate();
}
create database Andrei; GO; use Andrei;
The code that is generated is
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 };
}
}
}
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/EmbedResourceCSharp
Leave a Reply