i have been looking for help with this now for days and still not able to get this working. I want to make a function to get each line of my txt file and place it in its relavent var.

IE
DIM ServerV = (firstline of txt file)
dim DatabaseV = (second line of txt file)


I have a text file
Server=Localhost
Database=MaintSystem
User=mbish
Pass=mbish
serverid=1
email=mke1981@gmail.com

please please pleae can someone help me with this? i am using viusal basic 2010 vb.net

thanks in advanced

Recommended Answers

All 6 Replies

here is the code i have that inserts the first line of text into a text box hope this helps

Dim FILE_NAME As String = "C:\test.txt"
        Dim TextLine As String
        If System.IO.File.Exists(FILE_NAME) = True Then
            Dim objReader As New System.IO.StreamReader(FILE_NAME)
            Do While objReader.Peek() <> -1
                TextLine = TextLine & objReader.ReadLine()
            Loop
            TextBox1.Text = TextLine
        Else
            MsgBox("File Does Not Exist")
        End If

Here you go. I added lots of comments so the code should be self explanatory.

Dim FileName As String = "C:\Temp\test.txt"
Dim TextFromTheFile As String ' Whole text
Dim Lines() As String ' File splitted to lines
Dim LineSeparator() As String = {Environment.NewLine} ' Line separator 'character'

' Use My namespace
If My.Computer.FileSystem.FileExists(FileName) Then
    ' Read the file
    TextFromTheFile = My.Computer.FileSystem.ReadAllText(FileName)
    ' Get lines. I used StringSplitOptions that removes empty lines
    Lines = TextFromTheFile.Split(LineSeparator, System.StringSplitOptions.RemoveEmptyEntries)
    ' Now you have Lines() array ready
    TextBox1.Text = Lines(0)
    TextBox2.Text = Lines(1)
    ' and so on...
Else
    ' The file not found error
    MessageBox.Show("File Does Not Exist", _
                    "File Error", _
                    MessageBoxButtons.OK, _
                    MessageBoxIcon.Exclamation)
End If

Happy New Year 2011

wow thank you so much for the code.
hope you too have a good 2011.

i know its a little cheeky, but would you know how to only get
the text after the = on each line?

You got the answer in vb.net strings thread.

I would use Dim Parts() As String = Lines(0).Split("="c) approach. And I disagree with lolafuertes that this fails (assuming Lines(0) is not empty string).

Check the upper bound of the array:

Dim Parts() As String = Lines(0).Split("="c)
If Parts.GetUpperBound(0) = 1 Then
  ' Parts(0) has the left side of the "=" character and Parts(1) has the right side
Else
  ' Parts.GetUpperBound(0) = 0 i.e. Lines(0) did not have "=" character
  ' Parts(0) contains Lines(0)
End If

Could you please mark the thread as solved. Thank you!

See if this helps.

Public Class Form1
    Private myCoolFile As String = "C:\test.txt" '// your file.
    Private myCoolFileLines() As String = Nothing '// String Array to read File Lines into.
    Private sTemp As String = Nothing '// temp String to be used as needed.

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If IO.File.Exists(myCoolFile) Then '// check if File Exists.
            myCoolFileLines = IO.File.ReadAllLines(myCoolFile) '// load each line from your File as a String Array.
            TextBox1.Text = readFileLine(0) '// line 1 returned from Function.
            TextBox2.Text = readFileLine(1) '// line 2 returned from Function.
        End If
    End Sub

    Function readFileLine(ByVal selectedLine As Integer) As String
        '// get .Substring of selected line, starting from the "=".
        sTemp = myCoolFileLines(selectedLine).Substring(myCoolFileLines(selectedLine).IndexOf("=") + 1)
        Return sTemp '// Return .Substring.
    End Function
End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.