Files
RadioDJViewer/RadioDJViewer/EmbeddedResourceHelper.cs

42 lines
1.5 KiB
C#

using System;
using System.IO;
using System.Linq;
using System.Reflection;
namespace RadioDJViewer
{
public static class EmbeddedResourceHelper
{
/// <summary>
/// Reads an embedded resource's text content by matching the resource name ending with the provided file name.
/// Example: call Read("index.html") to load a resource named "RadioDJViewer.Web.index.html" or similar.
/// </summary>
public static string Read(string fileName)
{
if (string.IsNullOrEmpty(fileName))
throw new ArgumentNullException(nameof(fileName));
var asm = Assembly.GetExecutingAssembly();
var resourceName = asm.GetManifestResourceNames()
.FirstOrDefault(n => n.EndsWith(fileName, StringComparison.OrdinalIgnoreCase));
if (resourceName == null)
{
var names = string.Join(", ", asm.GetManifestResourceNames());
throw new FileNotFoundException($"Embedded resource '{fileName}' not found. Available resources: {names}");
}
using (var stream = asm.GetManifestResourceStream(resourceName))
{
if (stream == null)
throw new FileNotFoundException($"Resource stream '{resourceName}' is null.");
using (var reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
}
}