2014-07-04 54 views
3

這是我第一次使用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 
+1

您不爲'FileSystemWatcher' - 'Changed','Created','Deleted','Renamed'設置任何事件處理程序。它觸發 - 當它觸發時,它沒有什麼可以做**的。 – Tim

+1

'Dim pFiles As FileSystemWatcher'覆蓋了一個聲明爲WithEvents的函數。該行應該只是'pFiles = New FileSystemWatcher' – Plutonix

+0

等等,我認爲這是在添加「Handles pFiles.Created」時發生的情況?我認爲它在創建時運行Sub? – user287848

回答

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 

即使你手動把它掛使用AddHandlerpFiles超出範圍在窗體加載結束。正確的語法是:

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

啊,你說得對。我認爲這是因爲Sub是私人的,是的?我想我只是盲目地去,因爲我看到一些MSDN代碼示例把它放在Form Load中。 我會盡量修復它。 – user287848

+0

不,DIM僅在該過程中聲明新變量。你已經在頂部聲明瞭它,所以你只需要在Form load中創建一個實例。變量'範圍'由他們聲明的位置決定(Dim,Private,Friend,Public) – Plutonix

+0

哦,我明白我做了什麼。我沒有訪問類級別的pFiles變量,我創建了一個獨有的表單加載...謝謝指出。確切地說, – user287848