Results 1 to 6 of 6

Thread: [RESOLVED] Help with reading/writing text file, and deleting specific lines

  1. #1
    Lively Member
    Join Date
    Apr 09
    Posts
    69

    Resolved [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?

  2. #2
    Addicted Member EilaDoll's Avatar
    Join Date
    Dec 11
    Posts
    144

    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.
    http://www.vbforums.com/image.php?type=sigpic&userid=142423&dateline=1337150730

  3. #3
    Addicted Member EilaDoll's Avatar
    Join Date
    Dec 11
    Posts
    144

    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:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         'I used try simply because I have my original write function here
    3.         'but it is always a good idea to have a try-catch when accessing
    4.         'files.
    5.         Try
    6.             'Create a list of strings from the file
    7.             Dim iCount As New List(Of String)(IO.File.ReadAllLines("Test.txt"))
    8.  
    9.             'Count how many items are in the list and
    10.             'react accourdingly
    11.             If iCount.Count = 10 Then
    12.  
    13.                 'Remove the first item from iCount
    14.                 iCount.RemoveAt(0)
    15.  
    16.                 'Add the new time stamp to the bottom
    17.                 iCount.Add(My.Computer.Clock.LocalTime)
    18.  
    19.                 'If the count isn't 10 then you want to just add a new item
    20.             Else
    21.                 iCount.Add(My.Computer.Clock.LocalTime)
    22.             End If
    23.  
    24.             'Write the new values to the text file
    25.             IO.File.WriteAllLines("Test.txt", iCount)
    26.  
    27.             'Catch your exceptions
    28.             'I just had mine write the file with the current time
    29.         Catch ex As IO.FileNotFoundException
    30.             IO.File.WriteAllText("Test.txt", My.Computer.Clock.LocalTime)
    31.         End Try
    32.  
    33.         'This line was for debugging, it displays the text
    34.         'So I could count the lines
    35.         TextBox1.Text = IO.File.ReadAllText("Test.txt")
    36.     End Sub

    Feel free to take the example and adapt it how you need.
    http://www.vbforums.com/image.php?type=sigpic&userid=142423&dateline=1337150730

  4. #4
    Lively Member
    Join Date
    Apr 09
    Posts
    69

    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

  5. #5
    Lively Member
    Join Date
    Apr 09
    Posts
    69

    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

  6. #6
    Addicted Member EilaDoll's Avatar
    Join Date
    Dec 11
    Posts
    144

    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.
    http://www.vbforums.com/image.php?type=sigpic&userid=142423&dateline=1337150730

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •