954,517 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Read from text to textbox

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

Line 162 reads ServerAdmin [email]you@yourdomain.com[/email]

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?

aristosv
Newbie Poster
9 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 
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....

kingsonprisonic
Posting Whiz in Training
265 posts since Nov 2009
Reputation Points: 47
Solved Threads: 53
 

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.39KB
aristosv
Newbie Poster
9 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

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
aristosv
Newbie Poster
9 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

Is your problem solved???

kingsonprisonic
Posting Whiz in Training
265 posts since Nov 2009
Reputation Points: 47
Solved Threads: 53
 

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.

aristosv
Newbie Poster
9 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

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()
kingsonprisonic
Posting Whiz in Training
265 posts since Nov 2009
Reputation Points: 47
Solved Threads: 53
 

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

aristosv
Newbie Poster
9 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

Copy all content from the file and then replace old to new . now write all content to the same file..... Simple.

kingsonprisonic
Posting Whiz in Training
265 posts since Nov 2009
Reputation Points: 47
Solved Threads: 53
 

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()
kingsonprisonic
Posting Whiz in Training
265 posts since Nov 2009
Reputation Points: 47
Solved Threads: 53
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: