Using .txt Resource File in VB

Right now I have a line of code in vb that calls a text file, for example:

Dim fileReader As String fileReader = My.Computer.FileSystem.ReadAllText("data5.txt") 

data5.txt is a resource in my application, however the application does not start because it cannot find data5.txt. I am sure there is another code to search for the .txt file in the resource that I am viewing, but I cannot figure out how to figure it out. So does anyone know a simple solution about this? or maybe another whole new line of code? Thanks in advance!

+4
source share
2 answers

If you added the file as a resource on the tab "Projects + Properties", "Resources", you will get its contents using My.Resources:

 Dim content As String = My.Resources.data5 

Click the arrow on the Add resource button and select Add an existing file, select the data5.txt file.

+16
source

I assume the file is compiling as an embedded resource.

Embedded resources are not files in the file system; this code will not work.

You need to call Assembly.GetManifestResourceStream , for example:

 Dim fileText As String Dim a As Assembly = GetType(SomeClass).Assembly Using reader As New StreamReader(a.GetManifestResourceStream("MyNamespace.data5.txt")) fileText = reader.ReadToEnd() End Using 
+3
source

Source: https://habr.com/ru/post/1307275/


All Articles