-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResourcesUtilities.cs
99 lines (86 loc) · 3.66 KB
/
ResourcesUtilities.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
using System.IO;
using System.Linq;
using System.Reflection;
#nullable enable
namespace NetStandard20.Utilities;
public static class ResourcesUtilities
{
/// <summary>
/// Searches for a file among Embedded resources <br/>
/// Throws an <see cref="ArgumentException"/> if nothing is found or more than one match is found <br/>
/// <![CDATA[Version: 1.0.0.4]]> <br/>
/// </summary>
/// <param name="name"></param>
/// <param name="assembly"></param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
/// <returns></returns>
public static Stream ReadFileAsStream(string name, Assembly? assembly = null)
{
name = name ?? throw new ArgumentNullException(nameof(name));
assembly ??= Assembly.GetExecutingAssembly();
try
{
return assembly.GetManifestResourceStream(
assembly
.GetManifestResourceNames()
.Single(resourceName => resourceName.EndsWith(name, StringComparison.InvariantCultureIgnoreCase)))
?? throw new ArgumentException($"\"{name}\" is not found in embedded resources");
}
catch (InvalidOperationException exception)
{
throw new ArgumentException(
"Not a single one was found or more than one resource with the given name was found. " +
"Make sure there are no collisions and the required file has the attribute \"Embedded resource\"",
exception);
}
}
/// <summary>
/// Searches for a file among Embedded resources <br/>
/// Throws an <see cref="ArgumentException"/> if nothing is found or more than one match is found <br/>
/// <![CDATA[Version: 1.0.0.2]]> <br/>
/// <![CDATA[Dependency: ReadFileAsStream(string name, Assembly? assembly = null)]]> <br/>
/// </summary>
/// <param name="name"></param>
/// <param name="assembly"></param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
/// <returns></returns>
public static string ReadFileAsString(string name, Assembly? assembly = null)
{
name = name ?? throw new ArgumentNullException(nameof(name));
using var stream = ReadFileAsStream(name, assembly);
using var reader = new StreamReader(stream);
return reader.ReadToEnd();
}
/// <summary>
/// Searches for a file among Embedded resources <br/>
/// Throws an <see cref="ArgumentException"/> if nothing is found or more than one match is found <br/>
/// <![CDATA[Version: 1.0.0.2]]> <br/>
/// <![CDATA[Dependency: ReadFileAsStream(string name, Assembly? assembly = null)]]> <br/>
/// </summary>
/// <param name="name"></param>
/// <param name="assembly"></param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
/// <returns></returns>
public static byte[] ReadFileAsBytes(string name, Assembly? assembly = null)
{
name = name ?? throw new ArgumentNullException(nameof(name));
using var stream = ReadFileAsStream(name, assembly);
using var memoryStream = new MemoryStream();
stream.CopyTo(memoryStream);
return memoryStream.ToArray();
}
/// <summary>
/// Returns all names
/// </summary>
/// <param name="assembly"></param>
/// <returns></returns>
public static string[] GetResourcesNames(Assembly? assembly = null)
{
assembly ??= Assembly.GetExecutingAssembly();
return assembly.GetManifestResourceNames();
}
}