Jump to content

  •  

  • iBotModz CBox


    Photo

    [Request] Extract a resource.


    • Please log in to reply
    2 replies to this topic

    #1 ixGAMEOVERxixx

    ixGAMEOVERxixx

      Private Grade 2

    • VIP

    • 478 posts
    • Joined: 27-October 08
    • Gender:Not Telling
    • Location:Custom Games

    Posted 04 October 2009 - 12:08 PM

    I need some code which allows you to extract a resource from my application. I know you can run a .exe embeded as a resource in an application.. so extracting should be easy as well.

    #2 iBotPeaches

    iBotPeaches

      General Grade 6

    • Owner

    • 6,570 posts
    • Joined: 29-July 07
    • Gender:Male
    • Location:Kansas

    Posted 05 October 2009 - 05:56 AM

    I use a function for that. I think this is it.


    /// <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);
        }
    }
    


    #3 ixGAMEOVERxixx

    ixGAMEOVERxixx

      Private Grade 2

    • VIP

    • 478 posts
    • Joined: 27-October 08
    • Gender:Not Telling
    • Location:Custom Games

    Posted 05 October 2009 - 03:02 PM

    Never mind got it to work, thanks anyway peaches.