| | |
2008 Opening, editing and saving text files
Please support our VB.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2008
Posts: 44
Reputation:
Solved Threads: 0
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.
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)
Dim FILE_NAME As String = "C:\cat4\text.txt" Dim TextLine As String If System.IO.File.Exists(FILE_NAME) = True Then Dim objReader As New System.IO.StreamReader(FILE_NAME) Do While objReader.Peek() <> -1 TextLine = TextLine & objReader.ReadLine() & vbNewLine Loop RichTextBox1.Text = TextLine Else MsgBox("File Does Not Exist") End If End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Dim mydoclocation As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments RichTextBox1.SaveFile(mydoclocation & "c:\cat4\text.txt", RichTextBoxStreamType.PlainText) End Sub
You have problem with saving/loading text files, right?
I tested your save-code:
and the result was
and when you read the file
You may also want to check what else exists in the My-namespace. For example,
I tested your save-code:
VB.NET Syntax (Toggle Plain Text)
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click ' Dim mydoclocation As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments Dim TempPath As String TempPath = mydoclocation & "c:\cat4\text.txt" Debug.Print(TempPath) 'RichTextBox1.SaveFile(TempPath, RichTextBoxStreamType.PlainText) End Sub
"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)
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click ' Dim mydoclocation As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments RichTextBox1.SaveFile(mydoclocation & "\text.txt", RichTextBoxStreamType.PlainText) End Sub
VB.NET Syntax (Toggle Plain Text)
Dim FILE_NAME As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments FILE_NAME = FILE_NAME & "\text.txt"
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. Teme64 @ Windows Developer Blog
•
•
Join Date: Mar 2008
Posts: 44
Reputation:
Solved Threads: 0
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)
Private Const _FILE_NAME As String = "C:\cat4\bannedwebsites.txt" Private Const _FILE_NAME2 As String = "C:\cat4\bannedprograms.txt" Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BannedWebsite.Click Savedata.Show() RichTextBox1.Show() Information.Text = ("Please enter each seperate website on a new line") If My.Computer.FileSystem.FileExists(_FILE_NAME) = True Then RichTextBox1.Text = My.Computer.FileSystem.ReadAllText(_FILE_NAME, System.Text.Encoding.ASCII) Else : MessageBox.Show("File Does Not Exist") End If End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Savedata.Click Dim sMyDoclocation As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments RichTextBox1.SaveFile(_FILE_NAME, RichTextBoxStreamType.PlainText) End Sub
In that case I would read the file with System.IO.StreamReader
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.
VB.NET Syntax (Toggle Plain Text)
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click ' Dim TempPath As String Dim OneLine As String Dim objReader As System.IO.StreamReader Dim Lines As Integer TempPath = "D:\temp\foo.txt" Lines = 1 If My.Computer.FileSystem.FileExists(TempPath) Then objReader = New System.IO.StreamReader(TempPath) Do Until objReader.EndOfStream OneLine = objReader.ReadLine If (Lines Mod 2) = 0 Then ' Even lines RichTextBox2.AppendText(OneLine) RichTextBox2.AppendText(Environment.NewLine) Else ' Odd lines RichTextBox1.AppendText(OneLine) RichTextBox1.AppendText(Environment.NewLine) End If Lines += 1 Loop objReader.Close() objReader = Nothing End If ' Write two richtextboxes to same file Dim AllText As String = "" Dim Lines1 As Integer Dim Lines2 As Integer Lines1 = 0 Lines2 = 0 Do Until Lines1 > RichTextBox1.Lines.GetUpperBound(0) AndAlso Lines2 > RichTextBox2.Lines.GetUpperBound(0) If Lines1 <= RichTextBox1.Lines.GetUpperBound(0) Then AllText = AllText & RichTextBox1.Lines(Lines1) & Environment.NewLine Lines1 += 1 End If If Lines2 <= RichTextBox1.Lines.GetUpperBound(0) Then AllText = AllText & RichTextBox2.Lines(Lines2) & Environment.NewLine Lines2 += 1 End If Loop My.Computer.FileSystem.WriteAllText(TempPath, AllText, False) End Sub
You may also try My.Computer.FileSystem.ReadAllText to read the text file and then use Split method to get an array of lines.
Teme64 @ Windows Developer Blog
![]() |
Other Threads in the VB.NET Forum
- Previous Thread: vb.net setfocus
- Next Thread: send text to browser text field
| Thread Tools | Search this Thread |
Tag cloud for VB.NET
.net .net2008 2008 access add advanced application array basic beginner browser button buttons center click client code combo convert cpu cuesent data database datagrid datagridview datetimepicker designer dissertation dissertations dissertationtopic eclipse employees excel exists filter forms function html images lib listview map mobile module msaccess mysql net number open page pan panel pdf picturebox picturebox2 port position print printing printpreview problem read regex reuse right-to-left save search serial settings shutdown socket sorting sqldatbase sqlserver storedprocedure textbox timer timespan transparency txttoxmlconverter usercontol vb vb.net vb2008 vba vbnet vista visual visualbasic visualbasic.net visualstudio.net visualstudio2008 web webbrowser winforms winsock wpf wrapingcode xml year






