Click here to Skip to main content
11,920,353 members (52,806 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


5 bookmarked

Extract Resources from an Assembly

, 18 May 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Demonstrates how to retrieve resources from a .NET Assembly in Visual Basic .NET


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


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 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:


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

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


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


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
        If _events Is Nothing Then
            _events = New EventHandlerList()
        End If
        Return _events
    End Get
End Property

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 

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 

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

Public Property Search As Boolean Implements IGrabber.Search

Grabber Events


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

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

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


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

Public Sub Grab()
End Sub

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))
End Sub

' rest of other overloaded Methods, code goes here .........

Represents public method used to cancel or stop grab operation.

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

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
    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!!!!!



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.


  • Release, May 18, 2014


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Egypt Egypt
Oil & Gas Engineer
C# &
Coding For Fun Only

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151120.1 | Last Updated 18 May 2014
Article Copyright 2014 by a_pess
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid