我在使用FileSystemWatcher [vb.net]时遇到问题 [英] I am having trouble using FileSystemWatcher [vb.net]

查看:91
本文介绍了我在使用FileSystemWatcher [vb.net]时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

我还注意到有人说FSW出现故障或有问题.如果您认为我还应该使用其他东西,请告诉我.

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

推荐答案

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

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

即使您要使用AddHandler手动将其连接, pFiles在Form Load结束时也会超出范围.正确的语法为:

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(...

这篇关于我在使用FileSystemWatcher [vb.net]时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆