EmbeddedResources
This commit is contained in:
parent
e6407060a1
commit
d63d4eb7d8
27
DTLib/Filesystem/EmbeddedResources.cs
Normal file
27
DTLib/Filesystem/EmbeddedResources.cs
Normal file
@ -0,0 +1,27 @@
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
|
||||
namespace DTLib.Filesystem;
|
||||
|
||||
public static class EmbeddedResources
|
||||
{
|
||||
public static Stream GetResourceStream(string resourcePath, Assembly assembly = null) =>
|
||||
(assembly ?? Assembly.GetCallingAssembly())
|
||||
.GetManifestResourceStream(resourcePath)
|
||||
?? throw new Exception($"embedded resource <{resourcePath}> not found");
|
||||
|
||||
public static byte[] ReadBynary(string resourcePath, Assembly assembly = null)
|
||||
{
|
||||
if (assembly == null) assembly = Assembly.GetCallingAssembly();
|
||||
using var reader = new BinaryReader(GetResourceStream(resourcePath, assembly));
|
||||
return reader.ReadBytes(int.MaxValue);
|
||||
}
|
||||
|
||||
|
||||
public static string ReadText(string resourcePath, Assembly assembly = null)
|
||||
{
|
||||
if (assembly == null) assembly = Assembly.GetCallingAssembly();
|
||||
using var reader = new StreamReader(GetResourceStream(resourcePath, assembly));
|
||||
return reader.ReadToEnd();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user