Using resource files in Visual Basic 2008

Posted on 2008-11-06
Medium Priority
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
  • 2
LVL 25

Accepted Solution

Luis Pérez earned 1500 total points
ID: 22903067
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

ID: 22904918
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
ID: 22906175
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

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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month16 days, 2 hours left to enroll

850 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