Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Extract Resources from an Assembly

0.00/5 (No votes)
18 May 2014 1  
Demonstrates how to retrieve resources from a .NET Assembly in Visual Basic .NET

Introduction

This tip demonstrates how to retrieve resources from a .NET Assembly and the purpose is to share and learn.

Background

Recently, for fun only and to do something useful in my spare time, I was looking for a library to retrieve all resource files from a .NET Assembly in Visual Basic.NET? Actually, it is not quite difficult to handle resources files from Windows Form’s project and there are so many ways to deal with resources where you can store several type of files such as but not limited to........ dll, exe, text, image, xml ...and.......so on. The question was how I can do it?

Why Shall I Use Resources

May be it is seems like a silly question for some coders but actually it is not, because I saw so many questions here and there asking about resources, how to extract and how to handle it. Anyhow, my understanding to such a question is quite simple, resources representing data might be needed by an application to be stored or embedded directly into your application or by any other means.

However, since there is no library written particularly to Visual Basic.NET, I have decided to give it a try then started read here and there and finally was able to do it, and here it is the library, it is dedicated to all Visual Basic .NET community.

Classes of the Library

These are the classes and interfaces which make up the resources Grabber:

IGrabber

Represents the base interface of the Grabber class.

Imports System.ComponentModel

Public Interface IGrabber
    ReadOnly Property Events() As EventHandlerList
    Sub GrabResources(pathLocation As String, fileWildCard As String, grab As IGrabber)
    Sub GrabResources(fileName As String, grab As IGrabber)
    Sub GrabResources(grab As IGrabber)
    Sub GrabResources(data As Object)
    Sub CancelGrabbing()
    '// accessible properties of the grabber
    Property Folder As String
    Property Search As Boolean
    Property File As String
End Interface ' IGrabber
Grabber

Represents the class that is used to retrieve resources from an Assembly. Please refer to the attached file.

Delegates

Represents the delegates will occur as required by the Grabber class. Public Delegate Sub

Public Delegate Sub GrabCompletedEventHandler(sender As Object, e As EventArgs)

Public Delegate Sub GrabStartedEventHandler(sender As Object, fileName As String)

Public Delegate Sub GrabExceptionEventHandler(sender As Object, ex As Exception)

Grabber Class

The idea behind the Grabber class is to load an assembly and extract any resources within it and save under your application directory.

Grabber Constructor

#Region " constructor "

    Public Sub New()
        Me.Search = False
        Me.File = CType(Nothing, String)
        Me.Folder = CType(Nothing, String)
    End Sub

#End Region

    ' rest of the code goes here .........

Grabber Properties

Events

Represents EventHandlerList property used to store Grabber’s Delegates; it cannot be changed by derived classes.

Refer to: Declare Custom Events to Conserve Memory

Also, you may refer to my article in CodeProjet; Add Custom Event to a Class in VB.NET.

    Public ReadOnly Property Events As ComponentModel.EventHandlerList Implements IGrabber.Events
        Get
            If _events Is Nothing Then
                _events = New EventHandlerList()
            End If
            Return _events
        End Get
    End Property 
Folder

Represents String property used to get or set the location where the grabbed resources are to be saved, it can be changed by derived classes.

Public Property Folder As String Implements IGrabber.Folder 
File

Represents String property used to get/set the assembly file name used to grab resources from it and can be changed by derived classes.

Public Property File As String Implements IGrabber.File 
Search

Represents Flag property to check sub-folders of Assembly file.

Public Property Search As Boolean Implements IGrabber.Search

Grabber Events

GrabCompleted

Represents a Custom Event occurs when grab is completed.

    Public Custom Event GrabCompleted As GrabCompletedEventHandler
        AddHandler(value As GrabCompletedEventHandler)
            Me.Events.AddHandler("GrabCompletedEvent", value)
        End AddHandler

        RemoveHandler(value As GrabCompletedEventHandler)
            Me.Events.RemoveHandler("GrabCompletedEvent", value)
        End RemoveHandler

        RaiseEvent(sender As Object, e As EventArgs)
            CType(Me.Events("GrabCompletedEvent"), GrabCompletedEventHandler).Invoke(sender, e)
        End RaiseEvent
    End Event 
GrabStarted

Represents the Custom Event occurs when grab is started.

    Public Custom Event GrabStarted As GrabStartedEventHandler
        AddHandler(value As GrabStartedEventHandler)
            Me.Events.AddHandler("GrabStartedEvent", value)
        End AddHandler

        RemoveHandler(value As GrabStartedEventHandler)
            Me.Events.RemoveHandler("GrabStartedEvent", value)
        End RemoveHandler

        RaiseEvent(sender As Object, fileName As String)
            CType(Me.Events("GrabStartedEvent"), GrabStartedEventHandler).Invoke(sender, fileName)
        End RaiseEvent
    End Event 
GrabberException

Represents Custom Event occurs when an exception is thrown during grabbing.

    Public Custom Event GrabberException As GrabExceptionEventHandler
        AddHandler(value As GrabExceptionEventHandler)
            Me.Events.AddHandler("GrabberExceptionEvent", value)
        End AddHandler

        RemoveHandler(value As GrabExceptionEventHandler)
            Me.Events.RemoveHandler("GrabberExceptionEvent", value)
        End RemoveHandler

        RaiseEvent(sender As Object, ex As Exception)
            CType(Me.Events("GrabberExceptionEvent"), GrabExceptionEventHandler).Invoke(sender, ex)
        End RaiseEvent
    End Event 

Grabber Methods

Grab

Represents public method used to grab or retrieve resources from an Assembly.

    Public Sub Grab()
        Me.GrabResources(Me)
    End Sub
GrabResources

Represents overloaded friend methods used to load an assembly and grab or retrieve resources from it.

    Friend Sub GrabResources(grab As IGrabber) Implements IGrabber.GrabResources
        _thread = New Thread(New ParameterizedThreadStart(AddressOf GrabResources))
        _thread.Start(grab)
    End Sub
 
    ' rest of other overloaded Methods, code goes here ......... 
CancelGrabbing

Represents public method used to cancel or stop grab operation.

    Public Sub CancelGrabbing() Implements IGrabber.CancelGrabbing
        Me._cancelGrabbing = True
    End Sub 
GetFileName

Represents public shared method to get file name from a string.

    Public Shared Function GetFileName(fileNameString As String) As String
        If Not String.IsNullOrEmpty(fileNameString) Then
            Dim currentFileString As String = IO.Path.GetFileName(fileNameString)
            If currentFileString.EndsWith(".DLL", True, _
            CultureInfo.InvariantCulture) OrElse currentFileString.EndsWith_
            (".EXE", True, CultureInfo.InvariantCulture) Then
                currentFileString = currentFileString.Substring(0, currentFileString.Length - 4)
            End If
            Return currentFileString
        End If
        Return Nothing
    End Function 

Using the Code

    Private resGrabber As Grabber
    Private IsGrab As Boolean = False

    Private Sub StartGrabbing()

        resGrabber = New Grabber
        ' set required data
        resGrabber.File = textBoxSource.Text
        resGrabber.Search = True

        Dim folderName As String = "Grabber"
        Dim folderText As String = IO.Path.GetDirectoryName(New Uri_
        (Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath) & "\" & folderName

        ' Assign location og Grab Folder
        resGrabber.Folder = folderName & "\" & Grabber.GetFileName(resGrabber.File)

        ' Grab resources files
        IsGrab = True
        resGrabber.Grab()
        AddHandler resGrabber.GrabberException, New GrabExceptionEventHandler(AddressOf OnException)
        AddHandler resGrabber.GrabStarted, New GrabStartedEventHandler(AddressOf OnGrab)
        AddHandler resGrabber.GrabCompleted, New GrabCompletedEventHandler(AddressOf OnCompleted)

    End Sub

    ' rest of the code goes here..........
    ' 

For further details, please download the attached source code which shows how to do it ……. I hope you enjoy it!!!!!

References

  1. http://www.codeproject.com/Articles/229122/Add-Custom-Event-To-A-Class-in-VB-Net
  2. http://msdn.microsoft.com/en-us/library/vstudio/system.threading.parameterizedthreadstart
  3. http://msdn.microsoft.com/en-us/library/yt1k2w4e.aspx
  4. http://msdn.microsoft.com/en-us/library/70s77c20%28v=vs.110%29.aspx
  5. http://support.microsoft.com/kb/837908
  6. http://www.codeproject.com/Articles/13573/Extracting-Embedded-Images-From-An-Assembly
  7. http://www.nirsoft.net/utils/dot_net_resources_extract.html

Points of Interest

I know the library is not perfect. It has been collected from my readings and study of different similar codes in the network, but the benefits behind such a code is; now I have a good idea how to protect the .NET library and hope to share with you ASAP.

History

  • Release, May 18, 2014

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here