zoukankan      html  css  js  c++  java
  • Getting an embedded resource file out of an assembly (转载)

    Getting an embedded resource file out of an assembly
    posted on Wednesday, August 03, 2005 by bobby @ 4:04 pm
    I always embed supporting files in my applications and assemblies (images, scripts, xml docs, etc). This makes it so you don't have to package these files alongside your application or assembly. This isn't as necessary when you are writing an application (as most apps already need an installer), but it's extremely useful when developing server controls - which end up being assemblies and don't always warrant their own installers.

    Embedding files within an assembly is pretty straightforward. Add the file to your project, go to properties and change the Build Action to "Embedded Resource". Done.

    Super sweet right? So how the funk do I extract them out when I need to use them? With the code below foolio:

    Copy code to clipboard in IE or select code for Firefox
    /// <summary>
    /// Extracts an embedded file out of a given assembly.
    /// </summary>
    /// <param name="assemblyName">The namespace of you assembly.</param>
    /// <param name="fileName">The name of the file to extract.</param>
    /// <returns>A stream containing the file data.</returns>
    public static Stream GetEmbeddedFile(string assemblyName, string fileName)
    {
        try
        {
            System.Reflection.Assembly a = System.Reflection.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(assemblyName + ": " + e.Message);
        }
    }

    A couple of notes about the embedding resources. If you're like me and you like to organize your resources, you prolly have them grouped into folders. In that case, the fileName parameter must include the path to the file. For example, if I have the following embedded resource:

    Project Root/resources/images/top_left_corner.gif

    the fileName arg would be:

    myNamespace.resources.images.top_left_corner.gif

    Don't ask me why you use a period instead of a typical directory separator. Mine is not to question why.

    Perhaps you don't know the name of the assembly, or are just too lazy to pass it along, here are some overloads that take in alternative types - Type & Assembly

    Copy code to clipboard in IE or select code for Firefox
    public static Stream GetEmbeddedFile(System.Reflection.Assembly assembly, string fileName)
    {
        string assemblyName = assembly.GetName().Name;
        return GetEmbeddedFile(assemblyName, fileName);
    }
    
    public static Stream GetEmbeddedFile(Type type, string fileName)
    {
        string assemblyName = type.Assembly.GetName().Name;
        return GetEmbeddedFile(assemblyName, fileName);
    }
    So the following example returns a stream. But if you are constantly extracting XML docs, or bitmaps, wrap it up:

    For Bitmaps:
    Copy code to clipboard in IE or select code for Firefox
    public static Bitmap GetEmbeddedImage(Type type, string fileName)
    {
        Stream str = GetEmbeddedFile(type, fileName);
        return new Bitmap(str);
    }


    For XML Docs:
    Copy code to clipboard in IE or select code for Firefox
    public static XmlDocument GetEmbeddedXml(Type type, string fileName)
    {
        Stream str = GetEmbeddedFile(type, fileName);
        XmlTextReader tr = new XmlTextReader(str);
        XmlDocument xml = new XmlDocument();
        xml.Load(tr);
        return xml;
    }
    Be careful when embedding A LOT of files as it will significantly increase the size of your assembly.
     
  • 相关阅读:
    C#编程总结(三)线程同步 多线程
    配置 Spring.NET
    C# Redis
    WPF MvvmLight RelayCommand 绑定Command 的使用
    WCF编程系列(一)初识WCF
    C#/WPF程序开机自动启动
    C# 创建Windows Service(Windows服务)程序
    前端Js框架汇总
    列式存储和行式存储
    llvm-3.4.2 编译失败 解决方案
  • 原文地址:https://www.cnblogs.com/wuhenke/p/1660340.html
Copyright © 2011-2022 走看看