Forked from davidalpert/ManifestResourceHelper.cs
Last active
December 10, 2016 05:32
-
-
Save dalpert-korewireless/10685db7c62939873e23d8709d408145 to your computer and use it in GitHub Desktop.
Helper code to extract resources from a C# assembly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections.Generic; | |
| using System.IO; | |
| using System.Reflection; | |
| namespace YOUR_NAMESPACE | |
| { | |
| public static class ManifestResourceHelper | |
| { | |
| public static string[] GetManifestResourceNames(Assembly asm = null) | |
| { | |
| asm = asm ?? Assembly.GetCallingAssembly(); | |
| return asm.GetManifestResourceNames(); | |
| } | |
| public static IEnumerable<string> GetManifestResourcePaths(Assembly asm = null) | |
| { | |
| asm = asm ?? Assembly.GetCallingAssembly(); | |
| return asm.GetManifestResourceNames(); | |
| } | |
| public static ExcelPackage ExtractExcelPackage(string relativePathToResource) | |
| { | |
| var pkg = new ExcelPackage(); | |
| using (var stream = ExtractResourceStream(relativePathToResource)) | |
| { | |
| pkg.Load(stream); | |
| } | |
| return pkg; | |
| } | |
| public static FileInfo ExtractResourceToDisk(string relativePathToResource, string targetPathOnDisk = null, bool overwrite = true) | |
| { | |
| targetPathOnDisk = targetPathOnDisk ?? Path.GetTempFileName(); | |
| var file = new FileInfo(targetPathOnDisk); | |
| if (File.Exists(file.FullName) && overwrite) | |
| { | |
| File.Delete(file.FullName); | |
| } | |
| var stream = ExtractResourceStream(relativePathToResource, Assembly.GetCallingAssembly()); | |
| var content = StreamToContentString(stream); | |
| File.WriteAllText(file.FullName, content); | |
| return file; | |
| } | |
| public static Stream ExtractResourceStream(string relativePathToResource, Assembly asm = null) | |
| { | |
| return GetManifestResourceStream(relativePathToResource, asm); | |
| } | |
| private static string GetFullResourceName(string relativePathToResource, Assembly asm = null) | |
| { | |
| asm = asm ?? Assembly.GetCallingAssembly(); | |
| return asm.GetName().Name + "." + relativePathToResource.Replace('/', '\\').Replace('\\', '.'); | |
| } | |
| private static Stream GetManifestResourceStream(string resourceName, Assembly asm) | |
| { | |
| asm = asm ?? Assembly.GetCallingAssembly(); | |
| resourceName = GetFullResourceName(resourceName); | |
| var resourceInfo = asm.GetManifestResourceInfo(resourceName); | |
| if (resourceInfo == null) | |
| { | |
| var message = String.Format("Could not find the requested resource '{0}' among: {1}", resourceName, Environment.NewLine); | |
| var names = asm.GetManifestResourceNames(); | |
| message += string.Join(";" + Environment.NewLine, names); | |
| throw new FileNotFoundException(message, resourceName); | |
| } | |
| var manifestResourceStream = asm.GetManifestResourceStream(resourceName); | |
| return manifestResourceStream; | |
| } | |
| private static string StreamToContentString(Stream resourceStream) | |
| { | |
| if (resourceStream == null) return ""; | |
| using (var reader = new StreamReader(resourceStream)) | |
| { | |
| return reader.ReadToEnd(); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment