2008 Opening, editing and saving text files

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 44
Reputation: smelf1 is an unknown quantity at this point 
Solved Threads: 0
smelf1 smelf1 is offline Offline
Light Poster

2008 Opening, editing and saving text files

 
0
  #1
Jan 13th, 2009
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.
  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
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: 2008 Opening, editing and saving text files

 
0
  #2
Jan 13th, 2009
And The Problem Is.....?
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 276
Reputation: rapture has a spectacular aura about rapture has a spectacular aura about 
Solved Threads: 37
rapture rapture is offline Offline
Posting Whiz in Training

Re: 2008 Opening, editing and saving text files

 
0
  #3
Jan 13th, 2009
wow smelf1 you're just full of questions lately.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: 2008 Opening, editing and saving text files

 
0
  #4
Jan 13th, 2009
You have problem with saving/loading text files, right?

I tested your save-code:
  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
  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
  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 44
Reputation: smelf1 is an unknown quantity at this point 
Solved Threads: 0
smelf1 smelf1 is offline Offline
Light Poster

Re: 2008 Opening, editing and saving text files

 
0
  #5
Jan 13th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 44
Reputation: smelf1 is an unknown quantity at this point 
Solved Threads: 0
smelf1 smelf1 is offline Offline
Light Poster

Re: 2008 Opening, editing and saving text files

 
0
  #6
Jan 13th, 2009
Originally Posted by rapture View Post
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.

  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
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 44
Reputation: smelf1 is an unknown quantity at this point 
Solved Threads: 0
smelf1 smelf1 is offline Offline
Light Poster

Re: 2008 Opening, editing and saving text files

 
0
  #7
Jan 13th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: 2008 Opening, editing and saving text files

 
0
  #8
Jan 14th, 2009
In that case I would read the file with System.IO.StreamReader
  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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the VB.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC