[RESOLVED] Help with reading/writing text file, and deleting specific lines
Hello,
I am writing entries to a text file, to use as a buffer of the last ten messages sent in a simple chat program. I write the entries as they are sent/received to a text file named as a specific user. This way when that user's chat window is opened again, the last ten messages can be filled into the history text box. I know how to add lines to the file just fine, I am using this to make the entries when sending a message:
Code:
Using sw As New StreamWriter(strFile, True)
sw.WriteLine("• " & Sender & "(" & DateTime.Now & "): " & Message)
End Using
I don't know if it would be necessary or not, but I placed that character at the beginning of the string to serve as a marker, in case someone's message was so long it wraps to another line - does that even happen with stream writer?
Anyway, what I would like to add in is before writing a new line - count how many lines or marker characters are already in that file and if there are already ten, delete the top line. This way when the current line is added, it maintains that there will never be more than 10 lines in the file. Can someone point me in the right direction?
Re: Help with reading/writing text file, and deleting specific lines
You don't need to make the marker. The word wrap function you're talking about is for user interface only. The data will be written as one solid string. If you open notepad and go to "Format" and turn off word wrap, you'll see it as all consistent strings instead of wrapped to the size of the window.
What I would suggest for what you want to do (Mind you I'm not pro or anything.) I would use IO.File.ReadAllLines to read the whole thing as seperate strings by line, put that into an array, count the array, then replace array strings accourdingly. I did something close to this in a recent program I was working with, I'll try to pull up the code in a bit to show you how I did it.
Re: Help with reading/writing text file, and deleting specific lines
Ok, I wrote a code because it was easier than hunting down the project so here is what I got:
vb Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'I used try simply because I have my original write function here
'but it is always a good idea to have a try-catch when accessing
'files.
Try
'Create a list of strings from the file
Dim iCount As New List(Of String)(IO.File.ReadAllLines("Test.txt"))
'Count how many items are in the list and
'react accourdingly
If iCount.Count = 10 Then
'Remove the first item from iCount
iCount.RemoveAt(0)
'Add the new time stamp to the bottom
iCount.Add(My.Computer.Clock.LocalTime)
'If the count isn't 10 then you want to just add a new item
Else
iCount.Add(My.Computer.Clock.LocalTime)
End If
'Write the new values to the text file
IO.File.WriteAllLines("Test.txt", iCount)
'Catch your exceptions
'I just had mine write the file with the current time
Catch ex As IO.FileNotFoundException
IO.File.WriteAllText("Test.txt", My.Computer.Clock.LocalTime)
End Try
'This line was for debugging, it displays the text
'So I could count the lines
TextBox1.Text = IO.File.ReadAllText("Test.txt")
End Sub
Feel free to take the example and adapt it how you need.
Re: Help with reading/writing text file, and deleting specific lines
Well, I read another post about deleting a specific line and decided to go for it. This is what I got:
Code:
If File.Exists(strFile) Then
Dim fileLines As New List(Of String)
fileLines.AddRange(File.ReadAllLines(strFile))
If fileLines.Count > 9 Then
fileLines.RemoveAt(0)
File.WriteAllLines(strFile, fileLines.ToArray)
End If
End If
Using sw As New StreamWriter(strFile, True)
sw.WriteLine(Sender & "(" & DateTime.Now & "): " & Message)
End Using
I don't know if this is the most efficient way, but it's working :)
Re: Help with reading/writing text file, and deleting specific lines
Thanks EilaDoll, looks pretty close to what I came up with too so that makes me feel better :)
Re: Help with reading/writing text file, and deleting specific lines
Yea it's essentially the same thing, google works miracles. ;D Mark post as resolved please.