Search code examples
vb6text-filesembedded-resource

Text file from resource file


I have a text file which I want to include in the resource file in my project

Right now I have included the text file via "Add Custom Resource".

When I run the project I read out the file from the resource file and save it to a tempory file.

LoadDataIntoFile "TXT", App.Path & "\temp.txt"

This uses the following function

Public Sub LoadDataIntoFile(DataName As String, FileName As String)
  Dim myArray() As Byte
  Dim myFile As Long
  If Dir(FileName) = "" Then
    myArray = LoadResData(DataName, "CUSTOM")
    myFile = FreeFile
    Open FileName For Binary Access Write As #myFile
    Put #myFile, , myArray
    Close #myFile
  End If
End Sub

Next, when I need the text file data I read in the temporay file

'read complete text file
intFile = FreeFile
Open strFile For Input As #intFile
  strData = Input(LOF(intFile), #intFile)
Close #intFile

I then can use the data from the text file without any problems.

It seems a bit awkward though to first read the data, then save to file, and then read the data from file

Is there a way to inlcude a text file in the resource file, and read it directly into a string variable when I need it?


Solution

  • I found a solution.

    Reading the text file from the resource file returns an array of bytes.

    To convert this array of bytes into a string I use:

    strData = StrConv(myArray, vbUnicode)
    

    I am not sure if it is the best way, but it does work :)