Read from text to textbox

Nucleus

Active member
Joined
May 24, 2005
Messages
30
Programming Experience
Beginner
I have a form with a textbox and I want that textbox to read from httpd.conf on line 162.

Line 162 reads ServerAdmin you@yourdomain.com

Shouldn’t this code work? Because it does not.

Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim apacheconf = "c:\httpd.conf"
Dim line() As String = IO.File.ReadAllLines(apacheconf)


TextBox1.Text = line(59)
End Sub
End Class

Also when the textbox reads the line, it must only display the email address and not the word “ServerAdmin” How can I do that?
 

Nucleus

Active member
Joined
May 24, 2005
Messages
30
Programming Experience
Beginner
Ok the reading and displaying to the textbox is done

VB.NET:
TextBox1.Text = mid(line(59),instr(line(59),"ServerAdmin")+len("ServerAdmin "),len(line(59))-instr(line(59),"ServerAdmin")+len("ServerAdmin"))

Now for writing back to the textfile if the textbox value is changed I've gotten as far as this

VB.NET:
Dim apacheconf = "C:\webserver\Apache2.2\conf\httpd.conf"
Dim Line() As String = System.IO.File.ReadAllLines(apacheconf) 

If TextBox1.Text.ToString Then
IO.File.WriteAllLines(apacheconf, Line(162)) 
End If

But it's not really working. What I'm I doing wrong?
 

nK0de

Active member
Joined
Jan 20, 2012
Messages
42
Location
Colombo, Sri Lanka
Programming Experience
1-3
first of all import the System.IO namespace to your project like this. So that you won't have to write it everywhere in your code.

Imports System.IO


Secondly, since your importing the .IO namespace, use it's StreamReader class to read the text from your file. (StreamReader Class (System.IO))

Dim fName As String = " D:\httpd.conf"
Dim testTxt As New StreamReader(fName)
Dim allRead As String = testTxt.ReadToEnd()     '
testTxt.Close()


And you don't have to go through all that trouble to get that text part. there's a great member of the Text namespace called RegularExpressions in VB.NET which will help you to match predefined words/texts from files. This is how you import it to your project and use it.

Imports System.Text.RegularExpressions

'declare the RegEx field pattern.
Dim regMatch As Regex = New Regex("ServerAdmin")


'matching the data from the text file with the RegEx pattern
Dim mtch As Match = regMatch.Match(allRead)


If mtch.Success Then
      MessageBox.Show("found")
      TextBox1.Text = mtch.Groups(0).Value
Else
      MessageBox.Show("not found")
End If


and to writing to the file part. Use the counterpart of StreamReader which is StreamWriter. (StreamWriter Class (System.IO))

Dim writer As New StreamWriter(fName)


writer.Write(TextBox1.Text)
writer.Close()
MessageBox.Show("Text written to file", "Data Saved", MessageBoxButtons.OK, MessageBoxIcon.Information)
 

Nucleus

Active member
Joined
May 24, 2005
Messages
30
Programming Experience
Beginner
Yes you are right. But then we got into the writing back to the file. But you have a point, so i wont continue this discussion.
 
Top Bottom