What I'm trying to do is monitor a chat log for a game that I play. I want to make a program that will notify me anytime my character name has been mentioned in the chat. If my name is mentioned in the chat it tells me. Then it tells me again after it scans the file again over and over again. Every time the file changes. I only want to be notified once of each instance of my name being mentioned. Can someone help me correct this code to do what I need?
Private Sub FileSystemWatcher1_Changed(sender As Object, e As IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed
If e.FullPath = "C:\Games\Chat\Logs\chat.txt" Then
Dim fileContents As String = My.Computer.FileSystem.ReadAllText(e.FullPath)
' Convert the text to lower case to provide for better trigger matching if case sensitivity
' isn't an issue.
If fileContents.ToLower.Contains("palumbo") Then
Dim strMessage As String
strMessage = "Name Mentioned In Chat!" & vbNewLine
txtMessage.SelectionColor = Color.Red
txtMessage.SelectionFont = New Font("Arial", 18, FontStyle.Bold)
txtMessage.AppendText(strMessage)
End If
End If
End Sub
The way this is working is it checks the file sees that it was mentioned then it tells me. Then if the file changes anything at all added to the file it scans it again and tells me again. Even if my name hasn't been mentioned again. I'm not sure what code I need to add to only tell me once each time its mentioned even if my name is mentioned 100 times. I only want it to tell me each time it's mentioned. Then wait for the next. Maybe by checking the line number when my name is mentioned and if the number is greater then the last time mentioned then tell me if not then skip it?
I'm not sure how to do that though.
You can keep a count of the number of occurrences of your character name and be notified when that changes. Also, if you read from a file that changes often, you're likely to get an IOException
so one way around it is to make a copy and read that.
Private Sub FileSystemWatcher1_Changed(sender As Object, e As IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed
If e.FullPath = "C:\Games\Chat\Logs\chat.txt" Then
'temp file in different dir from the one being monitored
Dim tmpfile As String = "C:\chat.txt"
FileCopy(e.FullPath, tmpfile)
Dim fileContents As String = My.Computer.FileSystem.ReadAllText(tmpfile)
Static chatNameOccurences As Integer = 0
Dim chatName As String = "palumbo"
' Convert the text to lower case to provide for better trigger matching if case sensitivity
' isn't an issue.
If fileContents.ToLower.Contains(chatName) Then
Dim occurrences As Integer = CountMatches(fileContents.ToLower, chatName)
If Not occurrences.Equals(chatNameOccurences) Then
chatNameOccurences = occurrences
Dim strMessage As String
strMessage = "Name Mentioned In Chat " & chatNameOccurences & " times!" & vbNewLine
txtMessage.SelectionColor = Color.Red
txtMessage.SelectionFont = New Font("Arial", 18, FontStyle.Bold)
txtMessage.AppendText(strMessage)
End If
End If
End If
End Sub
Public Function CountMatches(ByVal value As String, ByVal ch As String) As Integer
Return New System.Text.RegularExpressions.Regex(ch).Matches(value).Count
End Function
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.