943,648 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 4835
  • VB.NET RSS
Jan 13th, 2009
0

2008 Opening, editing and saving text files

Expand Post »
Hi all,

I would like to be able to read data from a text file, be able to edit it and save the new edits to that file over writing the old ones.
VB.NET Syntax (Toggle Plain Text)
  1. Dim FILE_NAME As String = "C:\cat4\text.txt"
  2. Dim TextLine As String
  3.  
  4. If System.IO.File.Exists(FILE_NAME) = True Then
  5.  
  6. Dim objReader As New System.IO.StreamReader(FILE_NAME)
  7.  
  8. Do While objReader.Peek() <> -1
  9. TextLine = TextLine & objReader.ReadLine() & vbNewLine
  10. Loop
  11.  
  12. RichTextBox1.Text = TextLine
  13.  
  14. Else
  15.  
  16. MsgBox("File Does Not Exist")
  17.  
  18. End If
  19.  
  20. End Sub
  21.  
  22. Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
  23. Dim mydoclocation As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments
  24. RichTextBox1.SaveFile(mydoclocation & "c:\cat4\text.txt", RichTextBoxStreamType.PlainText)
  25. End Sub
Reputation Points: 10
Solved Threads: 0
Light Poster
smelf1 is offline Offline
44 posts
since Mar 2008
Jan 13th, 2009
0

Re: 2008 Opening, editing and saving text files

And The Problem Is.....?
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Jan 13th, 2009
0

Re: 2008 Opening, editing and saving text files

wow smelf1 you're just full of questions lately.
Reputation Points: 155
Solved Threads: 41
Posting Whiz in Training
rapture is offline Offline
294 posts
since Jul 2007
Jan 13th, 2009
0

Re: 2008 Opening, editing and saving text files

You have problem with saving/loading text files, right?

I tested your save-code:
VB.NET Syntax (Toggle Plain Text)
  1. Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  2. '
  3. Dim mydoclocation As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments
  4.  
  5. Dim TempPath As String
  6. TempPath = mydoclocation & "c:\cat4\text.txt"
  7. Debug.Print(TempPath)
  8. 'RichTextBox1.SaveFile(TempPath, RichTextBoxStreamType.PlainText)
  9.  
  10. End Sub
and the result was "C:\Documents and Settings\<user name>\My Documentsc:\cat4\text.txt" which obviously is not a valid file name, so the fixed code is
VB.NET Syntax (Toggle Plain Text)
  1. Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  2. '
  3. Dim mydoclocation As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments
  4.  
  5. RichTextBox1.SaveFile(mydoclocation & "\text.txt", RichTextBoxStreamType.PlainText)
  6.  
  7. End Sub
and when you read the file
VB.NET Syntax (Toggle Plain Text)
  1. Dim FILE_NAME As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments
  2. FILE_NAME = FILE_NAME & "\text.txt"
You may also want to check what else exists in the My-namespace. For example, If My.Computer.FileSystem.FileExists(FILE_NAME) Then and TextLine = My.Computer.FileSystem.ReadAllText(FILE_NAME, System.Text.Encoding.Default) if you want to read a text file with a single line of code.
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Jan 13th, 2009
0

Re: 2008 Opening, editing and saving text files

right i have it working now,

but how do i read say the first line in a text file and put it into a richtextbox1, then read the second line and put it into richtextbox2
Reputation Points: 10
Solved Threads: 0
Light Poster
smelf1 is offline Offline
44 posts
since Mar 2008
Jan 13th, 2009
0

Re: 2008 Opening, editing and saving text files

Click to Expand / Collapse  Quote originally posted by rapture ...
wow smelf1 you're just full of questions lately.

Ha ha yep, once i get going its hard to stop.

I have the text file reading into a richtextbox and saving back if i edit it.

VB.NET Syntax (Toggle Plain Text)
  1. Private Const _FILE_NAME As String = "C:\cat4\bannedwebsites.txt"
  2. Private Const _FILE_NAME2 As String = "C:\cat4\bannedprograms.txt"
  3. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BannedWebsite.Click
  4. Savedata.Show()
  5. RichTextBox1.Show()
  6. Information.Text = ("Please enter each seperate website on a new line")
  7. If My.Computer.FileSystem.FileExists(_FILE_NAME) = True Then
  8.  
  9. RichTextBox1.Text = My.Computer.FileSystem.ReadAllText(_FILE_NAME, System.Text.Encoding.ASCII)
  10. Else : MessageBox.Show("File Does Not Exist")
  11. End If
  12. End Sub
  13. Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Savedata.Click
  14. Dim sMyDoclocation As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments
  15. RichTextBox1.SaveFile(_FILE_NAME, RichTextBoxStreamType.PlainText)
  16. End Sub
Reputation Points: 10
Solved Threads: 0
Light Poster
smelf1 is offline Offline
44 posts
since Mar 2008
Jan 13th, 2009
0

Re: 2008 Opening, editing and saving text files

how do i put line 1 from a text file into richtextbox1 and line 2 from that text file into richtextbox2.

Then if i edit either i can then save and overwrite the original
Reputation Points: 10
Solved Threads: 0
Light Poster
smelf1 is offline Offline
44 posts
since Mar 2008
Jan 14th, 2009
0

Re: 2008 Opening, editing and saving text files

In that case I would read the file with System.IO.StreamReader
VB.NET Syntax (Toggle Plain Text)
  1. Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  2. '
  3. Dim TempPath As String
  4. Dim OneLine As String
  5. Dim objReader As System.IO.StreamReader
  6. Dim Lines As Integer
  7.  
  8. TempPath = "D:\temp\foo.txt"
  9. Lines = 1
  10.  
  11. If My.Computer.FileSystem.FileExists(TempPath) Then
  12. objReader = New System.IO.StreamReader(TempPath)
  13. Do Until objReader.EndOfStream
  14. OneLine = objReader.ReadLine
  15. If (Lines Mod 2) = 0 Then
  16. ' Even lines
  17. RichTextBox2.AppendText(OneLine)
  18. RichTextBox2.AppendText(Environment.NewLine)
  19. Else
  20. ' Odd lines
  21. RichTextBox1.AppendText(OneLine)
  22. RichTextBox1.AppendText(Environment.NewLine)
  23. End If
  24. Lines += 1
  25. Loop
  26. objReader.Close()
  27. objReader = Nothing
  28. End If
  29.  
  30. ' Write two richtextboxes to same file
  31. Dim AllText As String = ""
  32. Dim Lines1 As Integer
  33. Dim Lines2 As Integer
  34. Lines1 = 0
  35. Lines2 = 0
  36. Do Until Lines1 > RichTextBox1.Lines.GetUpperBound(0) AndAlso Lines2 > RichTextBox2.Lines.GetUpperBound(0)
  37. If Lines1 <= RichTextBox1.Lines.GetUpperBound(0) Then
  38. AllText = AllText & RichTextBox1.Lines(Lines1) & Environment.NewLine
  39. Lines1 += 1
  40. End If
  41. If Lines2 <= RichTextBox1.Lines.GetUpperBound(0) Then
  42. AllText = AllText & RichTextBox2.Lines(Lines2) & Environment.NewLine
  43. Lines2 += 1
  44. End If
  45. Loop
  46. My.Computer.FileSystem.WriteAllText(TempPath, AllText, False)
  47.  
  48. End Sub
Notice that the above code also merges and writes richtextboxes back to the text file, so you'll have to cut and paste that part to a separate procedure.

You may also try My.Computer.FileSystem.ReadAllText to read the text file and then use Split method to get an array of lines.
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: vb.net setfocus
Next Thread in VB.NET Forum Timeline: send text to browser text field





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC