using System; using System.IO; using System.Reflection; namespace Deltares.Standard { public static class Common { /// /// Extracts an embedded file out of a given assembly. /// /// The namespace of you assembly. /// The name of the file to extract. /// A stream containing the file data. public static Stream GetEmbeddedFile(string assemblyName, string fileName) { try { Assembly a = Assembly.Load(assemblyName); Stream str = a.GetManifestResourceStream(assemblyName + "." + fileName); if (str == null) throw new Exception("Could not locate embedded resource '" + fileName + "' in assembly '" + assemblyName + "'"); return str; } catch (Exception e) { throw new Exception(string.Format("{0}: {1}", assemblyName, e.Message)); } } /// /// Extracts an embedded file out of a given assembly. /// /// The name of the resource which normally is the namespace of you assembly and the file. /// A stream containing the file data. public static Stream GetEmbeddedFile(Assembly assembly, string resourceName) { try { Stream str = assembly.GetManifestResourceStream(resourceName); if (str == null) throw new Exception( string.Format("Could not locate embedded resource '{0}' in assembly '{1}'", resourceName, assembly.GetName())); return str; } catch (Exception e) { throw new Exception(string.Format("{0}: {1}", assembly.GetName(), e.Message)); } } } }