這是我第一次使用FileSystemWatcher,但它不工作。在監視路徑中創建文件時不會觸發。我的目標是監視程序文件目錄中的更改。我將比較在線列表(我下載)複製的文件。我還沒有完成那部分[如果它找到匹配,它會做什麼]。我究竟做錯了什麼?我無法使用FileSystemWatcher [vb.net]
我也注意到一些人說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
您不爲'FileSystemWatcher' - 'Changed','Created','Deleted','Renamed'設置任何事件處理程序。它觸發 - 當它觸發時,它沒有什麼可以做**的。 – Tim
'Dim pFiles As FileSystemWatcher'覆蓋了一個聲明爲WithEvents的函數。該行應該只是'pFiles = New FileSystemWatcher' – Plutonix
等等,我認爲這是在添加「Handles pFiles.Created」時發生的情況?我認爲它在創建時運行Sub? – user287848