Embedding a text file into your windows application

Category : VB.Net

This could be good for storing gathered data from your users and retaining it.

1. Go to your Projects property page by clicking ‘Project’ and the ‘Properties’.

2. From here click ‘Resources’, ‘Add Resource’, and then ‘Add new Text File’

3. Now back to your code behind you simply reference your text file as such:

        Dim username As String = TextBox1.Text
        Dim password As String = TextBox2.Text
 
        sw = New StreamWriter("My.Resources.Users", True)
        sw.WriteLine(username & password)
        sw.Close()

Notice the “My.Resources.Users” — Users.txt is what I named my text file so replace that name with your text file name.

Now you can write data to a simple text file (OBVIOUSLY) you would not want to store sensitive data in this BUT it does get compiled into the project so you will never need to worry about the package being installed in the wrong place. This text file will ALWAYS be with this program! Very nice…..

I read from the file like this :

 sr = New StreamReader("My.Resources.Users")
        Dim line As String
 
        Do
            line = sr.ReadLine
 
        Loop Until line Is Nothing
 
        sr.Close()

Cheers and caio!