• 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.
     
  • 相关阅读:
    进程之管道Pipe,数据共享Manager,进程池Poo
    进程之锁,信息量,事件,队列,生产者消费者模型,joinablequeue
    网络编程之sock server,自定义一个与sock server类相似的功能,支持多客户端通信
    网络编程之粘包解决方案
    进程之进程创建的两种方式,两种传值的方式,验证进程间数据隔离,join,守护进程,僵尸进程,孤儿进程
    毕业设计开题报告任务书参考文献格式和数量要求
    剑指offer-替换空格
    剑指offer-二维数组中的查找
    用forward和sendRedirect转发时的区别
    ServletContext实现显示用户在线人数
  • 原文地址:https://www.cnblogs.com/wuhenke/p/1660340.html
Copyright © 2020-2023  润新知