• 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.
     
  • 相关阅读:
    Leetcode 538. Convert BST to Greater Tree
    Leetcode 530. Minimum Absolute Difference in BST
    Leetcode 501. Find Mode in Binary Search Tree
    Leetcode 437. Path Sum III
    Leetcode 404. Sum of Left Leaves
    Leetcode 257. Binary Tree Paths
    Leetcode 235. Lowest Common Ancestor of a Binary Search Tree
    Leetcode 226. Invert Binary Tree
    Leetcode 112. Path Sum
    Leetcode 111. Minimum Depth of Binary Tree
  • 原文地址:https://www.cnblogs.com/wuhenke/p/1660340.html
Copyright © 2020-2023  润新知