Using resource files in Visual Basic 2008

Posted on 2008-11-06
Last Modified: 2013-11-26
Can anyone direct me to some tutorials on how to use resource files in Visual Basic 2008? I want to store a Word document, an empty Access database mdb and an HTML file in a resource file. I then want to either open these in a webbrowser control or save them to the hard disk. I am familiar with earlier vb programming, but this new paradigm has me struggling to keep up.
Question by:glenn_grubb
    LVL 25

    Accepted Solution

    Mmm... if you want to store as different files as MS Word, MS Access and so on, I think that would be better to use compressed files than resource files. Resource files (and .NET managed resource associated classes) are specially designed to manage resource-oriented types as strings, images (like icons, cursors, gifs), etc; but for complex types or "raw" file types, as you want, is better to use compressed files (for example, zip files, in which you may store any file type).

    Compression of .zip files in .NET can be easily accomplished by using SharpZipLib free open source library. You can find the library and documentation in:

    Hope that helps.

    LVL 27

    Expert Comment

    Having said that...  You can add each of these files to your project as resource, and then "extract" them and use them when needed.

    Here's an example of adding an Access database:
    Click on the Project menu | Add Existing Item...
      Navigate to and select the Access file.
        If the DataSource Configuration Wizard opens, just click Cancel.

    In Solution Explorer, Right-Click on the Access file, select Properties
      Set the Build Action to "Embedded Resource"

    Here's the code that you can use to "extract" the Access file:

            'Create a temporary path for our file (or change to a specific path)   'or .accdb
            Dim tempPath As String = IO.Path.ChangeExtension(IO.Path.GetTempFileName(), ".mdb")

            'Get the Current Assembly information
            Dim currentAssembly As Reflection.Assembly = Reflection.Assembly.GetExecutingAssembly()

            'Resource will store the full name of our embeded Access resource file
            Dim resource As String = String.Empty

            'Get all resource names and find the full embeded dpe.exe resource file name
            Dim arrResources As String() = currentAssembly.GetManifestResourceNames()
            For Each resource In arrResources

                'or .accdb
                If resource.IndexOf(".mdb") > -1 Then _
                    Exit For


            'Open the Embeded Access resource
            Dim resourceStream As IO.Stream = currentAssembly.GetManifestResourceStream(resource)

            'Open FileStream to write out
            Dim writer As New IO.FileStream(tempPath, IO.FileMode.Create, IO.FileAccess.Write)

            'Write out all bytes of the stream
            Const size As Int16 = 4096
            Dim bytes(size) As Byte
            Dim numBytes As Int32 = 0

                numBytes = resourceStream.Read(bytes, 0, size)

                writer.Write(bytes, 0, numBytes)

            Loop While (numBytes > 0)

            'Close both streams and clean up


    LVL 25

    Expert Comment

    by:Luis Pérez
    Well, in fact I never said that this couln't be done, but I said that probably would be better to use zip files... and I forget to mention one more advantage: zip files are by definition encoded (more over if they are password-protected), but resource files store in exe file as-is, byte-sequential, if I'm not wrong.

    Author Comment

    The code works in that it it writes a file to the disk, but it is unreadable by Access. I tried the same thing with a word document (I changed the appropriate extensions) and the same thing happened in that the Word document opened but it was just gibberish.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    This video discusses moving either the default database or any database to a new volume.
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now