I have a form with a textbox and I want that textbox to read from http.conf on line 162.

Line 162 reads ServerAdmin you@yourdomain.com

Shouldn’t this code work?

Public Class Form1
    Dim apacheconf = "c:\httpd.conf"
    Dim line() As String = IO.File.ReadAllLines(apacheconf)

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        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?

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        TextBox1.Text = mid(line(59),instr(line(59),"ServerAdmin")+len("ServerAdmin"),len(line(59))-instr(line(59),"ServerAdmin")+len("ServerAdmin"))
    End Sub

try this....

It's working, but at the beggining it puts a space. (See attached screenshot).

Not that it's a problem, but at some point I will make it modify the text file when you change the email address in the textbox, and I dont know if that space will affect.

Attachments 1.JPG 5.39 KB

I just added a space after +len("ServerAdmin ")

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load        

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

End Sub

Yes. If it's not too difficult to tell me how to write back to the text file on that specific line, when the email address in the textbox is changed, I would really appreciate it.

just open the file
read all lines and save them into a string
now

str.replace("old@some.com","new@some.com")

now write the string to that file....

Dim fl As New System.IO.StreamReader("Path")
        Dim str = fl.ReadToEnd
        fl.Dispose()
str.replace("old@some.com","new@some.com")
        Dim sw As New System.IO.StreamWriter("Path")
        sw.Flush()
        sw.Write(str)
        sw.Dispose()

Yes but how does that relate to the textbox, and what's written in it?

I was thinking more like something like this:

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. :-/

Like

Dim fl As New System.IO.StreamReader("Path")
        Dim str = fl.ReadToEnd
        fl.Dispose()
        str.replace("old@some.com",Me.Textbox1.Text.Trim)
        Dim sw As New System.IO.StreamWriter("Path")
        sw.Flush()
        sw.Write(str)
        sw.Dispose()
This article has been dead for over six months. Start a new discussion instead.