I am trying to include resources via csproj file to a C# .NET 7 app and then use them (get a byte array or something), there are some resources online to this but none of them worked for me and many of them were outdated. I made some code how it probably like but all load resource functions I tried couldn't find a resource. (No errors or warnings)
dotnet publish -r win-x86 -c Release --self-contained true /p:PublishSingleFile=true
.cs
using System.Reflection;
using System.IO;
namespace Program{
class Program{
static void Main(){
GetRes("Program.Build2.png");
}
public static byte[] GetRes(string resName)
{
var assembly = typeof(MyLibrary.MyClass).GetTypeInfo().Assembly;
Stream resource = assembly.GetManifestResourceStream(resName);
return StreamToByteArray(resource);
}
public static byte[] StreamToByteArray(Stream input)
{
using (MemoryStream ms = new MemoryStream())
{
input.CopyTo(ms);
return ms.ToArray();
}
}
}
}
.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="this.txt" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Build2.png" />
</ItemGroup>
</Project>
I wanted to load resources but the functions I tried always returned null.