/// <summary> /// Return the content of a text file embed as a resource. /// The function takes care of finding the fully qualify name, in the current /// assembly. /// </summary> /// <param name="resourceFileName">The file name of the resource</param> /// <returns></returns> public static string GetTextResource(string resourceFileName, Assembly assembly) { bool gzip = false; TextResourceEncoding encoding = TextResourceEncoding.Unicode; var resourceFullName = GetResourceFullName(resourceFileName, assembly); if (gzip) { var buffer = GetBinaryResource(resourceFileName, assembly); var text = string.Empty; var bufferUnziped = buffer; switch (encoding) { case TextResourceEncoding.Ascii: text = Encoding.ASCII.GetString(buffer, 0, buffer.Length); break; case TextResourceEncoding.Unicode: text = Encoding.Unicode.GetString(buffer, 0, buffer.Length); break; case TextResourceEncoding.UTF8: text = Encoding.UTF8.GetString(buffer, 0, buffer.Length); break; } return(text); } else { using (var _textStreamReader = new StreamReader(assembly.GetManifestResourceStream(resourceFullName))) return(_textStreamReader.ReadToEnd()); } }
/// <summary> /// Return the content of a text file embed as a resource. /// The function takes care of finding the fully qualify name, in the current /// assembly. /// </summary> /// <param name="resourceFileName">The file name of the resource</param> /// <returns></returns> public static string GetTextResource(string resourceFileName, Assembly assembly, bool gzip = false, TextResourceEncoding encoding = TextResourceEncoding.Unicode) { var resourceFullName = GetResourceFullName(resourceFileName, assembly); if(gzip) { var buffer = GetBinaryResource(resourceFileName, assembly); var text = string.Empty; var bufferUnziped = Compression.GZip.Unzip(buffer); switch(encoding) { case TextResourceEncoding.Ascii : text = Encoding.ASCII.GetString(bufferUnziped);break; case TextResourceEncoding.Unicode: text = Encoding.Unicode.GetString(bufferUnziped);break; case TextResourceEncoding.UTF8 : text = Encoding.UTF8.GetString(bufferUnziped);break; } return text; } else { using (var _textStreamReader = new StreamReader(assembly.GetManifestResourceStream(resourceFullName))) return _textStreamReader.ReadToEnd(); } }
/// <summary> /// Return multiple text files embed as a resource in a dictionary. /// The key in the resource name, the value is the text data /// The function takes care of finding the fully qualify name, in the passed /// assembly. /// </summary> /// <param name="regex">The regular expression to filter the resource by name. The file system '\' are replaced with '.'</param> /// <returns></returns> public static Dictionary <string, string> GetTextResource(System.Text.RegularExpressions.Regex regex, Assembly assembly) { bool gzip = false; TextResourceEncoding encoding = TextResourceEncoding.Unicode; var dic = new Dictionary <string, string> (); var names = new List <string>(); foreach (var resource in assembly.GetManifestResourceNames()) { if (regex.IsMatch(resource)) { names.Add(resource); } } foreach (var name in names) { dic.Add(name, GetTextResource(name, assembly)); } return(dic); }
/// <summary> /// Return multiple text files embed as a resource in a dictionary. /// The key in the resource name, the value is the text data /// The function takes care of finding the fully qualify name, in the passed /// assembly. /// </summary> /// <param name="regex">The regular expression to filter the resource by name. The file system '\' are replaced with '.'</param> /// <returns></returns> public static Dictionary<string, string> GetTextResource(System.Text.RegularExpressions.Regex regex , Assembly assembly, bool gzip = false, TextResourceEncoding encoding = TextResourceEncoding.Unicode) { var dic = new Dictionary<string, string> (); var names = new List<string>(); foreach (var resource in assembly.GetManifestResourceNames()) if (regex.IsMatch(resource)) names.Add(resource); foreach(var name in names) dic.Add(name, GetTextResource(name, assembly, gzip, encoding)); return dic; }