Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Resources.cs
1using System;
2using System.IO;
3using System.Reflection;
4using System.Text;
6
7namespace Waher.Runtime.IO
8{
12 public static class Resources
13 {
20 public static byte[] LoadResource(string ResourceName)
21 {
22 return LoadResource(ResourceName, Types.GetAssemblyForResource(ResourceName));
23 }
24
32 public static byte[] LoadResource(string ResourceName, Assembly Assembly)
33 {
34 using (Stream f = Assembly.GetManifestResourceStream(ResourceName))
35 {
36 if (f is null)
37 throw new ArgumentException("Resource not found: " + ResourceName, nameof(ResourceName));
38
39 if (f.Length > int.MaxValue)
40 throw new ArgumentException("Resource size exceeds " + int.MaxValue.ToString() + " bytes.", nameof(ResourceName));
41
42 int Len = (int)f.Length;
43 byte[] Result = new byte[Len];
44 f.ReadAll(Result, 0, Len);
45 return Result;
46 }
47 }
48
55 public static string LoadResourceAsText(string ResourceName)
56 {
57 return LoadResourceAsText(ResourceName, Types.GetAssemblyForResource(ResourceName));
58 }
59
67 public static string LoadResourceAsText(string ResourceName, Assembly Assembly)
68 {
69 using (Stream f = Assembly.GetManifestResourceStream(ResourceName))
70 {
71 if (f is null)
72 throw new ArgumentException("Resource not found: " + ResourceName, nameof(ResourceName));
73
74 if (f.Length > int.MaxValue)
75 throw new ArgumentException("Resource size exceeds " + int.MaxValue.ToString() + " bytes.", nameof(ResourceName));
76
77 int Len = (int)f.Length;
78 byte[] Result = new byte[Len];
79 f.ReadAll(Result, 0, Len);
80
81 return Strings.GetString(Result, Encoding.UTF8);
82 }
83 }
84
85 }
86}
Static class managing loading of resources stored as embedded resources or in content files.
Definition: Resources.cs:13
static string LoadResourceAsText(string ResourceName, Assembly Assembly)
Loads a text resource from an embedded resource.
Definition: Resources.cs:67
static string LoadResourceAsText(string ResourceName)
Loads a text resource from an embedded resource.
Definition: Resources.cs:55
static byte[] LoadResource(string ResourceName)
Loads a resource from an embedded resource.
Definition: Resources.cs:20
static byte[] LoadResource(string ResourceName, Assembly Assembly)
Loads a resource from an embedded resource.
Definition: Resources.cs:32
Static class managing binary representations of strings.
Definition: Strings.cs:10
static string GetString(byte[] Data, int Offset, int Count, Encoding DefaultEncoding)
Gets a string from its binary representation, taking any Byte Order Mark (BOM) into account.
Definition: Strings.cs:148
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:15
static Assembly GetAssemblyForResource(string ResourceName)
Gets the assembly corresponding to a given resource name.
Definition: Types.cs:1787