我在使用FileSystemWatcher [vb.net]时遇到问题

时间:2014-07-04 17:44:26

标签: vb.net filesystemwatcher

这是我第一次使用FileSystemWatcher,但它不起作用。在受监视的路径中创建文件时不会触发。我的目标是监视Program File目录中的更改。我将比较在线列表(我下载)复制的文件。我还没有完成那部分[如果找到匹配将会怎么做]。我做错了什么?

我也注意到有人说FSW很不好或有问题。如果您认为我应该使用别的东西,请告诉我。

Imports System.IO
Imports System.Net
Public Class Form1
Private WithEvents pFiles As FileSystemWatcher
Private WithEvents pFiles32 As FileSystemWatcher
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.WindowState = FormWindowState.Minimized
    Me.ShowInTaskbar = False

    pFiles = New FileSystemWatcher("C:\Program Files", "*.*")
    pFiles.IncludeSubdirectories = True
    If Environment.Is64BitOperatingSystem.Equals(True) Then
        pFiles32 = New FileSystemWatcher("C:\Program Files (x86)", "*.*")
        pFiles32.IncludeSubdirectories = True
    End If
End Sub

Sub badFiles(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles pFiles.Created
    MsgBox("Triggered in x64 folder!")
    Dim fileInfo = New FileInfo(e.FullPath)
    Dim createWord = fileInfo.Name.ToString()
    Dim myWebClient As New System.Net.WebClient
    myWebClient.DownloadFile("http://www.systemlookup.com/lists.php?list=1&type=filename&search=" & createWord & "&s=", "C:\Users\Tyler\Desktop\" & createWord & ".html")
    Dim reader = IO.File.ReadAllText("C:\Users\Tyler\Desktop\" & createWord & ".html")
    If reader.Contains("No results. Please try a different search term.") Then
        MsgBox("Not Found!")
    Else
        If reader.Contains(createWord) Then
            MsgBox("Found!")
        Else
            MsgBox("Not Found!")
        End If
    End If
End Sub

Sub badFiles32(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles pFiles32.Created
    MsgBox("Triggered in x86 folder!")
    Dim fileInfo = New FileInfo(e.FullPath)
    Dim createWord = fileInfo.Name.ToString()
    Dim myWebClient As New System.Net.WebClient
    myWebClient.DownloadFile("http://www.systemlookup.com/lists.php?list=1&type=filename&search=" & createWord & "&s=", "C:\Users\Tyler\Desktop\" & createWord & ".html")
    Dim reader = IO.File.ReadAllText("C:\Users\Tyler\Desktop\" & createWord & ".html")
    If reader.Contains("No results. Please try a different search term.") Then
        MsgBox("Not Found!")
    Else
        If reader.Contains(createWord) Then
            MsgBox("Found!")
        Else
            MsgBox("Not Found!")
        End If
    End If
End Sub
End Class

1 个答案:

答案 0 :(得分:0)

您似乎正在替换模块级变量:

Private WithEvents pFiles As FileSystemWatcher
Private WithEvents pFiles32 As FileSystemWatcher

Private Sub Form1_Load(ByVal sender As System.Object, 
      ByVal e As System.EventArgs) Handles MyBase.Load

    ' this creates a NEW pfiles which only exists in FormLoad
    Dim pFiles As FileSystemWatcher = New FileSystemWatcher("C:\Program Files", 
           "*.*")
    pFiles.IncludeSubdirectories = True

即使您使用AddHandler pFiles在表单加载结束时超出范围,也会手动将其挂起。正确的语法是:

' since it is already declared (DIM) you just need to instance it (NEW):
pFiles = New FileSystemWatcher(...