2using System.Reflection;
4using System.Security.Cryptography.X509Certificates;
6using System.Threading.Tasks;
24 string[] Parts = ResourceName.Split(
'.');
25 string ParentNamespace;
33 ParentNamespace = Parts[0];
42 ParentNamespace +=
"." + Parts[i];
50 throw new ArgumentException(
"Assembly not found for resource " + ResourceName +
".", nameof(ResourceName));
73 public static byte[]
LoadResource(
string ResourceName, Assembly Assembly)
75 using (Stream f = Assembly.GetManifestResourceStream(ResourceName))
78 throw new ArgumentException(
"Resource not found: " + ResourceName, nameof(ResourceName));
80 if (f.Length >
int.MaxValue)
81 throw new ArgumentException(
"Resource size exceeds " +
int.MaxValue.ToString() +
" bytes.", nameof(ResourceName));
83 int Len = (int)f.Length;
84 byte[] Result =
new byte[Len];
85 f.ReadAll(Result, 0, Len);
110 using (Stream f = Assembly.GetManifestResourceStream(ResourceName))
113 throw new ArgumentException(
"Resource not found: " + ResourceName, nameof(ResourceName));
115 if (f.Length >
int.MaxValue)
116 throw new ArgumentException(
"Resource size exceeds " +
int.MaxValue.ToString() +
" bytes.", nameof(ResourceName));
118 int Len = (int)f.Length;
119 byte[] Result =
new byte[Len];
120 f.ReadAll(Result, 0, Len);
144 public static X509Certificate2
LoadCertificate(
string ResourceName, Assembly Assembly)
169 public static X509Certificate2
LoadCertificate(
string ResourceName,
string Password, Assembly Assembly)
172 if (Password is
null)
173 return new X509Certificate2(Data);
175 return new X509Certificate2(Data, Password);
185 using (FileStream fs = File.OpenRead(FileName))
188 if (l >
int.MaxValue)
189 throw new NotSupportedException(
"File too large.");
192 byte[] Bin =
new byte[Len];
194 await fs.ReadAsync(Bin, 0, Len);
230 using (FileStream fs = File.Create(FileName))
232 await fs.WriteAsync(Data, Offset, Length);
255 using (FileStream fs = File.OpenWrite(FileName))
257 fs.Position = fs.Length;
258 await fs.WriteAsync(Data, Offset, Length);
280 using (FileStream fs = File.Create(FileName))
282 byte[] Preamble = Encoding.GetPreamble();
283 byte[] Data = Encoding.GetBytes(Text);
284 int i, c = Preamble.Length;
292 for (i = 0; i < c; i++)
294 if (Preamble[i] != Data[i])
300 await fs.WriteAsync(Preamble, 0, c);
303 await fs.WriteAsync(Data, 0, Data.Length);
Helps with parsing of commong data types.
static string GetString(byte[] Data, Encoding DefaultEncoding)
Gets a string from its binary representation, taking any Byte Order Mark (BOM) into account.
Static class managing loading of resources stored as embedded resources or in content files.
static byte[] LoadResource(string ResourceName, Assembly Assembly)
Loads a resource from an embedded resource.
static Assembly GetAssembly(string ResourceName)
Gets the assembly corresponding to a given resource name.
static async Task< byte[]> ReadAllBytesAsync(string FileName)
Reads a binary file asynchronously.
static async Task AppendAllBytesAsync(string FileName, byte[] Data, int Offset, int Length)
Appends a binary file asynchronously.
static X509Certificate2 LoadCertificate(string ResourceName)
Loads a certificate from an embedded resource.
static byte[] LoadResource(string ResourceName)
Loads a resource from an embedded resource.
static string LoadResourceAsText(string ResourceName)
Loads a text resource from an embedded resource.
static Task WriteAllBytesAsync(string FileName, byte[] Data)
Creates a binary file asynchronously.
static async Task WriteAllBytesAsync(string FileName, byte[] Data, int Offset, int Length)
Creates a binary file asynchronously.
static async Task< string > ReadAllTextAsync(string FileName)
Reads a text file asynchronously.
static Task AppendAllBytesAsync(string FileName, byte[] Data)
Appends a binary file asynchronously.
static X509Certificate2 LoadCertificate(string ResourceName, string Password)
Loads a certificate from an embedded resource.
static X509Certificate2 LoadCertificate(string ResourceName, string Password, Assembly Assembly)
Loads a certificate from an embedded resource.
static async Task WriteAllTextAsync(string FileName, string Text, Encoding Encoding)
Creates a text file asynchronously.
static string LoadResourceAsText(string ResourceName, Assembly Assembly)
Loads a text resource from an embedded resource.
static Task WriteAllTextAsync(string FileName, string Text)
Creates a text file asynchronously.
static X509Certificate2 LoadCertificate(string ResourceName, Assembly Assembly)
Loads a certificate from an embedded resource.
Static class that dynamically manages types and interfaces available in the runtime environment.
static bool IsSubNamespace(string Namespace, string LocalName)
Checks if a local name in LocalName represents a subnamespace from the point of view of the namespac...
static Assembly GetFirstAssemblyReferenceInNamespace(string Namespace)
Gets the assembly reference of the first type found in a namespace.
static bool IsRootNamespace(string Name)
Checks if a name is a root namespace.