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.
     
  • 相关阅读:
    触发器基本使用
    查询结果合并用逗号分隔
    查询报表增加小计功能
    sql语句格式化数字(前面补0)
    如何在选择画面中创建下拉列表(drop down list)-as list box
    如何更改函数的函数组(function group)
    ABAP语言中如何定义嵌套内表(nested internal table)
    [REUSE_ALV_GRID_DISPLAY]如何指定单元格颜色
    如何创建嵌套动态内表(Nested dynamic internal table)
    如何根据方法名(method)查找所在类(class)-SE84
  • 原文地址:https://www.cnblogs.com/wuhenke/p/1660340.html
Copyright © 2011-2022 走看看