forked from ddrilling/AsbCloudServer
50 lines
1.3 KiB
C#
50 lines
1.3 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace AsbCloudInfrastructure
|
|
{
|
|
public static class AssemblyExtensions
|
|
{
|
|
public static Stream GetTemplateCopyStream(this Assembly assembly, string templateName)
|
|
{
|
|
var resourceName = assembly
|
|
.GetManifestResourceNames()
|
|
.FirstOrDefault(n => n.EndsWith(templateName));
|
|
|
|
if (string.IsNullOrWhiteSpace(resourceName))
|
|
throw new ArgumentNullException(nameof(resourceName));
|
|
|
|
using var stream = Assembly.GetExecutingAssembly()
|
|
.GetManifestResourceStream(resourceName);
|
|
|
|
var memoryStream = new MemoryStream();
|
|
stream?.CopyTo(memoryStream);
|
|
memoryStream.Position = 0;
|
|
|
|
return memoryStream;
|
|
}
|
|
|
|
[Obsolete]
|
|
public static async Task<Stream> GetTemplateCopyStreamAsync(this Assembly assembly,
|
|
string templateName,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var resourceName = assembly
|
|
.GetManifestResourceNames()
|
|
.FirstOrDefault(n => n.EndsWith(templateName))!;
|
|
|
|
using var stream = Assembly.GetExecutingAssembly()
|
|
.GetManifestResourceStream(resourceName)!;
|
|
|
|
var memoryStream = new MemoryStream();
|
|
await stream.CopyToAsync(memoryStream, cancellationToken);
|
|
memoryStream.Position = 0;
|
|
|
|
return memoryStream;
|
|
}
|
|
}
|
|
} |