我有一个表单,可以检查文件夹中是否有一些更改。我用FileSystemWatcher来做这件事。当通知更改时,必须加载另一个表单。第二个窗体已加载,但控件不可见。当我通过单击按钮加载第二个窗体时,控件是可见的。我想这是因为If语句,但我不确定。有人能帮我解决这个问题吗?
Imports System.IO
'Imports System.Diagnostics
Public Class Settings
Public watchfolder As FileSystemWatcher
Private Sub Settings_Load(sender As Object, e As EventArgs) Handles MyBase.Load
txt_watchpath.Text = "D:\temp"
If txt_watchpath.Text = "" Then
btn_startwatch.Enabled = False
End If
End Sub
Private Sub txt_watchpath_TextChanged(sender As Object, e As EventArgs) Handles txt_watchpath.TextChanged
If Me.Text <> "" Then
btn_startwatch.Enabled = True
End If
End Sub
Private Sub btn_startwatch_Click(sender As Object, e As EventArgs) Handles btn_startwatch.Click
watchfolder = New System.IO.FileSystemWatcher()
'this is the path we want to monitor
watchfolder.Path = txt_watchpath.Text
'Add a list of Filter we want to specify
'make sure you use OR for each Filter as we need to
'all of those
watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or
IO.NotifyFilters.LastWrite
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or
IO.NotifyFilters.Attributes
' add the handler to each event
AddHandler watchfolder.Changed, AddressOf logchange
AddHandler watchfolder.Created, AddressOf logchange
'AddHandler watchfolder.Deleted, AddressOf logchange
' add the rename handler as the signature is different
AddHandler watchfolder.Renamed, AddressOf logrename
'Set this property to true to start watching
watchfolder.EnableRaisingEvents = True
watchfolder.IncludeSubdirectories = False
btn_startwatch.Enabled = False
btn_stop.Enabled = True
Me.Hide()
'End of code for btn_start_click
End Sub
Private Sub logchange(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)
If e.ChangeType = IO.WatcherChangeTypes.Changed Then
FrmPopup.Fullpath = e.FullPath
FrmPopup.Activity = e.Name & " is changed"
FrmPopup.Show()
End If
If e.ChangeType = IO.WatcherChangeTypes.Created Then
FrmPopup.Fullpath = e.FullPath
FrmPopup.Activity = e.Name & " is made"
test.Show()
End If
End Sub
Private Sub logrename(ByVal source As Object, ByVal e As System.IO.RenamedEventArgs)
FrmPopup.Fullpath = e.FullPath
FrmPopup.Activity = e.OldName & " is changed to " & e.Name
FrmPopup.Show()
End Sub
Private Sub btn_stop_Click(sender As Object, e As EventArgs) Handles btn_stop.Click
FrmPopup.Show()
End Sub
End Class发布于 2020-08-27 00:54:56
FileSystemWatcher事件不是由UI线程生成的。要进入你的UI线程,你需要使用类似这样的东西:
Private Sub logchange(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)
If InvokeRequired Then
Me.Invoke(New MethodInvoker(Sub()
If e.ChangeType = IO.WatcherChangeTypes.Changed Then
FrmPopup.Fullpath = e.FullPath
FrmPopup.Activity = e.Name & " is changed"
FrmPopup.Show()
End If
If e.ChangeType = IO.WatcherChangeTypes.Created Then
FrmPopup.Fullpath = e.FullPath
FrmPopup.Activity = e.Name & " is made"
test.Show()
End If
End Sub))
End If
End Sub
Private Sub logrename(ByVal source As Object, ByVal e As System.IO.RenamedEventArgs)
If InvokeRequired Then
Me.Invoke(New MethodInvoker(Sub()
FrmPopup.Fullpath = e.FullPath
FrmPopup.Activity = e.OldName & " is changed to " & e.Name
FrmPopup.Show()
End Sub))
End If
End Subhttps://stackoverflow.com/questions/63599185
复制相似问题