[英]How do I check a text file for a certain string and notify when that string has been added?
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.
我不确定我需要添加什么代码,即使我的名字被提到 100 次,每次提到它时也只告诉我一次。 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.此外,如果您从经常更改的文件中读取数据,您可能会收到
IOException
因此一种解决方法是复制并读取该文件。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.