问题
This is my first time using FileSystemWatcher, but it's not working. It doesn't trigger when a file is created in the monitored paths. My goal is to monitor changes in the Program File directories. I will compare files copied against an online list (which I download). I'm not finished with that part yet [what it will do if it finds a match]. What am I doing wrong?
I've also noticed some say that FSW is glitchy or has issues. If you think I should use something else, let me know.
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:
It appears you are replacing your module level variables:
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
Even you were to manually hook it up using AddHandler
, that pFiles
goes out of scope at the end of Form Load. The correct syntax would be:
' since it is already declared (DIM) you just need to instance it (NEW):
pFiles = New FileSystemWatcher(...
来源:https://stackoverflow.com/questions/24578937/i-am-having-trouble-using-filesystemwatcher-vb-net