BangumiRenamer/Src/Utils/ResourceLoader.cs
2025-10-30 23:01:44 +08:00

41 lines
1.2 KiB
C#

using System.Reflection;
using System.Text;
namespace BangumiRenamer.Utils;
public static class ResourceLoader
{
public static IEnumerable<string> GetAllResNames()
{
var assembly = Assembly.GetExecutingAssembly();
return assembly.GetManifestResourceNames();
}
public static async Task<string> ReadText(string resPath)
{
var stream = GetResStream(resPath);
if (stream.CanSeek)
{
stream.Position = 0;
}
using var reader = new StreamReader(stream, Encoding.UTF8);
return await reader.ReadToEndAsync();
}
public static Stream GetResStream(string resPath)
{
var assembly = Assembly.GetExecutingAssembly();
var resourceName = resPath;
if (assembly.GetManifestResourceInfo(resPath) == null)
{
resourceName = $"{assembly.GetName().Name}.{resourceName.Replace('\\', '.').Replace('/', '.')}";
}
var stream = assembly.GetManifestResourceStream(resourceName);
if (stream == null)
{
throw new FileNotFoundException($"资源 {resourceName} 未找到");
}
return stream;
}
}