file.text

starcraft

Centurion
Joined
Jun 29, 2003
Messages
167
Location
Poway CA
how to i display a text document inside a window on my program without having the text file be a separet file. what i mean is there a way to have the text file within the program and have it display it some how when called. If u cant have it within the program how do u display the text file from a separet file?

Ps. How do i create a text document with variables? like"window1 Title = "windowtitle" and add/subtract/call those variables?
 
Last edited:
well you can include a textfile in your project , but it will get stored in the program's folder anyway , but here's how to read from a textfile on your HD to a textbox :
Visual Basic:
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim f As IO.File
        Dim path As String = "C:\test.txt"
        If f.Exists(path) Then '/// check if the text file exsists.
            Dim sRead As IO.StreamReader = New IO.StreamReader(New IO.FileStream(path, IO.FileMode.Open))
            While Not sRead.Peek
                TextBox2.AppendText(sRead.ReadLine & Environment.NewLine) '/// read the text file 1 line at a time , in to a textbox.
            End While
            sRead.Close()
        Else
            MessageBox.Show("oops check you put the correct file path!")
        End If
    End Sub
 
also you can use a richtextbox to open the file with a very small amount of code :
Visual Basic:
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If IO.File.Exists("C:\test.txt") Then
            RichTextBox1.LoadFile("C:\test.txt", RichTextBoxStreamType.PlainText)
        End If
    End Sub
 
You can embed your text file into the exe, but you will only be able to read the text file, wont be able to write to it.

Add the file to your solution, go to your soltuion explorer and click on it once, down in the properties window you should see a Build Action property, set it to Embeded Resource. Now you text file will be compiled into the exe.
Then to read it do this:
Visual Basic:
Dim reader As New IO.StreamReader(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("RootNamespace.FileName.txt"))
This will retrieve the file for you to read.
 
Back
Top