简体   繁体   English

如何检查某个字符串的文本文件并在添加该字符串时通知?

[英]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.

相关问题 检查文本文件中的每一行是否都包含某个字符串,然后将其添加到列表框中? - Check if each line from a text file contains a certain string, and add the ones that do to a listbox? vb.net如何检查字符串是否有某个单词 - vb.net how to check if a string has a certain word 如何在vb.net中修复错误“连接字符串未正确初始化”? - How do I fix the error 'Connection string has not properly been initialized' in vb.net? 如何检查某个进程是否有焦点? - How do I check if a certain process has focus? 如何检查用户在个人资料中是否具有特定角色? - How do I check if a user has a certain role in their profile? 如何创建一个持续更新的文本框,以在添加项目后立即显示更新的小计? - How do I create a continuously updating Textbox that displays an updated subtotal as soon as an item has been added? 如何检查字符串中的特定后缀? - How can I check for a certain suffix in my string? 如何在文本文件中拆分逗号分隔的字符串并将每个字符串保存在不同的变量中? - How do I split the comma separated string in my text file and save each string in different variable? 如何检查字符串.net中某个位置的字符 - How to check characters at a certain position in a string .net 如何检查是否已安装IIS 6管理兼容性功能? - How do I check whether IIS 6 Management Compatibility feature has been installed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM