这是我第一次使用 FileSystemWatcher,但它不起作用。在受监视的路径中创建文件时不会触发它。我的目标是监视程序文件目录中的更改。我会将复制的文件与在线列表(我下载的)进行比较。我还没有完成该部分[如果找到匹配项它将做什么]。我究竟做错了什么?
我还注意到有人说 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
看来您正在替换模块级变量:
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
, that pFiles
在表单加载结束时超出范围。正确的语法是:
' since it is already declared (DIM) you just need to instance it (NEW):
pFiles = New FileSystemWatcher(...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)