Home All Groups Group Topic Archive Search About

How do I embed a text file in my exe?

Author
9 May 2006 12:38 AM
David
I have an application that creates a database from a text file of DDL
(generated by Visio). Currently I have to deploy the text file with my
application and open it using the statement: SQL =
My.Computer.FileSystem.ReadAllText("CreateDB.SQL").

This works fine, but I would prefer not to have to deploy a separate text
file, and I would like to prevent the users from having access to this file.

Is there some way to easily embed the text file into the compiled
executable? If there is, I will also need to know how to open and read from
an embedded file.

Author
9 May 2006 1:40 AM
jayeldee
David,

You can add the file to your project, then set its Build ACtion to
'Embedded Resource' through the IDE.  You can then access it using a
function like the one below (there may be a cleaner way but this is
what I've used for similar files in the past)

Note: The FileName variable will be "[NameSpace].[FileName]" where
NameSpace is the namespace of your project (if you have one defined) in
the project properties.  I just wrote up a console app and I had to use
"ConsoleApplication3.TextFile1" to stream it.

Function GetFileContents(ByVal FileName As String) As String

        Dim this As [Assembly]
        Dim fileStream As IO.Stream
        Dim streamReader As IO.StreamReader
        Dim strContents As String
        this = System.Reflection.Assembly.GetExecutingAssembly
        fileStream = this.GetManifestResourceStream(FileName)
        streamReader = New IO.StreamReader(fileStream)
        strContents = streamReader.ReadToEnd
        streamReader.Close()
        Return strContents

    End Function
Author
9 May 2006 3:44 AM
David
Thanks Jayeldee. I must have been posting my own reponse at the same time you
posted yours. I think we have the same approach but VS2005 allowed me to cut
a few corners.

Show quoteHide quote
"jayeldee" wrote:

> David,
>
> You can add the file to your project, then set its Build ACtion to
> 'Embedded Resource' through the IDE.  You can then access it using a
> function like the one below (there may be a cleaner way but this is
> what I've used for similar files in the past)
>
> Note: The FileName variable will be "[NameSpace].[FileName]" where
> NameSpace is the namespace of your project (if you have one defined) in
> the project properties.  I just wrote up a console app and I had to use
> "ConsoleApplication3.TextFile1" to stream it.
>
>  Function GetFileContents(ByVal FileName As String) As String
>
>         Dim this As [Assembly]
>         Dim fileStream As IO.Stream
>         Dim streamReader As IO.StreamReader
>         Dim strContents As String
>         this = System.Reflection.Assembly.GetExecutingAssembly
>         fileStream = this.GetManifestResourceStream(FileName)
>         streamReader = New IO.StreamReader(fileStream)
>         strContents = streamReader.ReadToEnd
>         streamReader.Close()
>         Return strContents
>
>     End Function
>
>
Author
9 May 2006 1:47 AM
David
Please ignore I worked this out myself as follows (to easy with VS2005):

- Open My Project and go to the Resources tab
- Click "Add Resource", then "Add Existing File" and select the DDL file
(in my case CreateDB.SQL)

The file is now an embedded resource and can be accessed as follows:

SQL = My.Resources.CreateDB

Note the file extension is dropped when it is added to the resources.

Show quoteHide quote
"David" wrote:

> I have an application that creates a database from a text file of DDL
> (generated by Visio). Currently I have to deploy the text file with my
> application and open it using the statement: SQL =
> My.Computer.FileSystem.ReadAllText("CreateDB.SQL").
>
> This works fine, but I would prefer not to have to deploy a separate text
> file, and I would like to prevent the users from having access to this file.
>
> Is there some way to easily embed the text file into the compiled
> executable? If there is, I will also need to know how to open and read from
> an embedded file.
>
Author
9 May 2006 3:46 AM
jayeldee
Sounds a lot easier to do in 2005.