为什么我的FileSystemWatcher事件没有激活? [英] Why isn't my FileSystemWatcher events fireing ?

查看:68
本文介绍了为什么我的FileSystemWatcher事件没有激活?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个活动Noob,我正在从我的主模块控制台应用程序中调用一个类
这样的
。很抱歉,如果这是很多代码要阅读,但根据我的书,我看不到

错误。

非常感谢。


如果有像我这样的其他新手可以随意使用我的代码,如果它是

任何用途:)


我已经在doWorkOnChangedFile设置了一个断点但是没有发生......我是

像我一样充斥着我的日志。

Dim oFileWatcher As new sysLogIoHandler

oFileWatcher.FileNamePathWithOutEndingSlash =" C:\Program

Files \Syslogd\Logs"

oFileWatcher.FileName =" SyslogCatchAll .txt"

oFileWatcher.Constructor()

Do while 1< 2

System.Threading.Thread.Sleep(400)

循环

这是我班上的一些代码。我现在调用Sub Constructor进行

调试。


''------------------ -----------------------

类sysLogIoHandler代码

''------ -----------------------------------


Public Sub Constructor ()


Dim oFileStream As System.IO.File

''确保文件支持我们以后要使用的属性

on然后设置文件的EOF位置

''检查文件是否存在

sCompleteFileName = sFilePath& " \" &安培; sFileName


如果File.Exists(sCompleteFileName)= False那么

oWatcherEventLogger.LogEvent(Me.EventLogName," File not not

存在或无法访问该文件:"& sFileName,EventLogEntryType.Error)

退出Sub

结束如果


''尝试打开FileStream

尝试

oWatcherFileSystemStream =(oFileStream.Open(sCompleteFileName,

FileMode.Open,FileAccess.Read ,FileShare.None))

Catch ex As Exception

oWatcherEventLogger.LogEvent(Me.EventLogName,ex.ToString,

EventLogEntryType.Error)

结束尝试


''检查fileStream是否支持寻求

如果oWatcherFileSystemStream.CanSeek = False那么

oWatcherEventLogger.LogEvent(Me.EventLogName,"文件流

不支持Seeking"& sCompleteFileName,EventLogEntryType.Error)

退出Sub

结束如果


''和文件属性的EOF,因为我们要从这里处理所有

的变化。

lEofPosition = getCurrentEOF()

doWatchFileForChanges()


结束子

私函数getStreamsCurrentEOF()As Long

getStreamsCurrentEOF = CLng(oWatcherFileSystemStream.Length)


结束函数


公共函数doWatchFileForChanges()As Boolean

Dim oWatcherFileWatcher As New FileSystemWatcher

' '设置我们要观察的路径

oWatcherFileWatcher.Path = sFilePath

''将过滤器设置为仅观看一个文件

oWatcherFileWatcher.Filter = sFileName

''注意增加大小或更改lastwrittenflag

oWatcherFileWatcher.NotifyFilter = NotifyFilters.LastWrite或

NotifyFilters.Size

''为我们想要处理的事件设置处理程序

AddHandler oWatcherFileWatcher.Changed,AddressOf

doWo rkOnChangedFile

AddHandler oWatcherFileWatcher.Deleted,AddressOf

doWorkOnChangedFile


''告诉FileWatcher开始工作和举办活动
oWatcherFileWatcher.EnableRaisingEvents = True


结束功能

Private Sub doWorkOnChangedFile(ByVal source As Object,ByVal e As

System.IO.FileSystemEventArgs)

Select Case e.ChangeType

Case WatcherChangeTypes.Changed

Console.Write(doReadChangesInFile)

Case WatcherChangeTypes.Deleted

Console.Write(文件已被删除)

结束选择

结束Sub


Private Sub doNotifyThatFileIsNoMore(ByVal source As Object,ByVal e As

System.Io.FileSystemEventArgs)


End Sub


私有函数doReadChangesInFile()As String

如果Me.getCurrentEOF = Me.getLastKnownEOF则

oWatcherEventLogger.LogEvent(Me .EventLogNa我,A'更改文件

事件''已触发,我正在尝试查找更改,但文件大小是

与上次我执行此操作相同 ; &安培; ,EventLogEntryType.Error)

返回"" ''没有数据返回

结束如果

Dim iNumberOfBytes作为整数

''获取我们应该读取的字节数

iNumberOfBytes = Me.getCurrentEOF - Me.getLastKnownEOF

''准备缓冲区以正确的大小读取数据

Dim abReaderBuffer(iNumberOfBytes)As Byte

''转到最后一次更改前的最后一次EOF

oWatcherFileSystemStream.Seek(Me.getLastKnownEOF,SeekOrigin.Begin)

''阅读数据进入我们的缓冲区

oWatcherFileSystemStream.Read(abReaderBuffer,0,iNumberOfBytes)

''设置下一个knownEOF,因为我们处理了已更改的数据

lEofPosition = getCurrentEOF()

返回Encoding.ASCII.GetString(abReaderBuffer)

结束函数

解决方案

最重要的是..你要用什么样的系统来运行它?


Kai Thorsrud < KA ****** @ thispartgoesaways.start.no>在留言中写道

news:u9 ************** @ TK2MSFTNGP09.phx.gbl ...

嗨我是一个事件Noob和我从我的主模块控制台调用一个类
app就像这样。很抱歉,如果这是很多代码要阅读,但根据我的书,我看不出
错误。
非常感谢。

如果有的话在这里像我这样的其他新手可以随意使用我的代码,如果它有任何用途:)

我在doWorkOnChangedFile设置了一个断点但是没有发生...我我好像充满了我的日志。

Dim oFileWatcher作为新的sysLogIoHandler
oFileWatcher.FileNamePathWithOutEndingSlash =" C:\Program
Files \ Syslogd \\ \\ Logs"
oFileWatcher.FileName =" SyslogCatchAll.txt"
oFileWatcher.Constructor()
Do While 1< 2
System.Threading.Thread.Sleep(400)
循环

这是我班上的一些代码。我现在打电话给Sub Constructor进行调试。

''-------------------------- ---------------
类sysLogIoHandler代码
''---------------------- -------------------
公共子构造函数()

Dim oFileStream As System.IO.File
''确保文件支持我们以后使用
的属性,然后设置文件的EOF位置
''检查文件是否存在
sCompleteFileName = sFilePath& " \" &安培; sFileName

如果File.Exists(sCompleteFileName)= False那么
oWatcherEventLogger.LogEvent(Me.EventLogName,文件不存在或无法访问该文件: & sFileName,EventLogEntryType.Error)
退出Sub
结束如果

''尝试打开FileStream
尝试
oWatcherFileSystemStream =
(oFileStream.Open(sCompleteFileName,FileMode.Open,FileAccess.Read,FileShare.None))
Catch ex As Exception
oWatcherEventLogger.LogEvent(Me.EventLogName,ex.ToString,
EventLogEntryType。错误)
结束尝试

''检查fileStream是否支持搜索
如果oWatcherFileSystemStream.CanSeek = False那么
oWatcherEventLogger.LogEvent(Me.EventLogName,& filestream
不支持Seeking"& sCompleteFileName,EventLogEntryType.Error)
退出Sub
结束如果

''设置文件属性的EOF,因为我们要从这里处理所有的
更改。
lEofPosition = getCurrentEOF()
doWatchFileForChanges()


私有函数getStreamsCurrentEOF()As get
getStreamsCurrentEOF = CLng(oWatcherFileSystemStream.Length)

>结束函数

公共函数doWatchFileForChanges()As Boolean
Dim oWatcherFileWatcher As New FileSystemWatcher
''设置我们要观察的路径
oWatcherFileWatcher.Path = sFilePath
''设置过滤器只看一个文件
oWatcherFileWatcher.Filter = sFileName
''注意增加大小或更改lastwrittenflag
oWatcherFileWatcher.NotifyFilter = NotifyFilters.LastWrite或
NotifyFilters.Size
''设置e的处理程序我们想要处理的通风口
AddHandler oWatcherFileWatcher.Changed,AddressOf
doWorkOnChangedFile
AddHandler oWatcherFileWatcher.Deleted,AddressOf
doWorkOnChangedFile

''告诉FileWatcher启动工作和举办活动
oWatcherFileWatcher.EnableRaisingEvents = True

结束职能
Private Sub doWorkOnChangedFile(ByVal source As Object,ByVal e As
System.IO.FileSystemEventArgs)
Select Case e.ChangeType
Case WatcherChangeTypes.Changed
Console.Write(doReadChangesInFile)
Case WatcherChangeTypes.Deleted
Console.Write(文件已被删除)
结束选择
结束子

私有子doNotifyThatFileIsNoMore(ByVal源为对象,ByVal e
As System.Io.FileSystemEventArgs)

结束子

私人功能doRe adChangesInFile()As String
如果Me.getCurrentEOF = Me.getLastKnownEOF然后
oWatcherEventLogger.LogEvent(Me.EventLogName,A''更改
文件事件''触发并且我是试图找到变化,但文件大小与上次我做的相同 &安培; ,EventLogEntryType.Error)
返回" ''没有数据返回
结束如果
Dim iNumberOfBytes作为整数
''获取我们应该读取的字节数
iNumberOfBytes = Me.getCurrentEOF - Me.getLastKnownEOF < br'>''准备缓冲区以正确的大小读取数据
Dim abReaderBuffer(iNumberOfBytes)As Byte
''转到最后一次更改之前的最后一次EOF
oWatcherFileSystemStream.Seek(Me .getLastKnownEOF,
SeekOrigin.Begin)''将数据读入我们的缓冲区
oWatcherFileSystemStream.Read(abReaderBuffer,0,iNumberOfBytes)
''设置下一个knownEOF,因为我们处理了已更改的数据
lEofPosition = getCurrentEOF()
返回Encoding.ASCII.GetString(abReaderBuffer)
结束函数



Windows XP专业版。它应该在完成后在Windows 2003服务器上运行

CJ Taylor <无**** @ blowgoats.com>在消息中写道

新闻:10 ************* @ corp.supernews.com ...

最重要的是..什么样的系统你想要运行它吗?

Kai Thorsrud < KA ****** @ thispartgoesaways.start.no>在消息中写道
新闻:u9 ************** @ TK2MSFTNGP09.phx.gbl ...

你好我是一个活动Noob和我正在从我的主模块控制台app

这样调用一个类。很抱歉,如果有很多代码要阅读,但根据我的书,我看不到
错误。
非常感谢。

如果有的话在这里像我这样的其他新手可以随意使用我的代码,如果它是
的任何用途:)

我在doWorkOnChangedFile设置了一个断点但是没有发生...我我好像充满了我的日志。

Dim oFileWatcher作为新的sysLogIoHandler
oFileWatcher.FileNamePathWithOutEndingSlash =" C:\Program
Files \ Syslogd \\ \\ Logs"
oFileWatcher.FileName =" SyslogCatchAll.txt"
oFileWatcher.Constructor()
Do While 1< 2
System.Threading.Thread.Sleep(400)
循环

这是我班上的一些代码。我现在打电话给Sub Constructor进行调试。

''-------------------------- ---------------
类sysLogIoHandler代码
''---------------------- -------------------
公共子构造函数()

Dim oFileStream As System.IO.File
''确保文件支持我们要使用的属性


以后

然后设置文件的EOF位置
''检查是否该文件存在
sCompleteFileName = sFilePath& " \" &安培; sFileName

如果File.Exists(sCompleteFileName)= False那么
oWatcherEventLogger.LogEvent(Me.EventLogName,文件确实
不存在或无法访问该文件: & sFileName,EventLogEntryType.Error)
退出Sub
结束如果

''尝试打开FileStream
尝试
oWatcherFileSystemStream =


(oFileStream.Open(sCompleteFileName,

FileMode.Open,FileAccess.Read,FileShare.None))
Catch ex As Exception
oWatcherEventLogger.LogEvent(我。 EventLogName,ex.ToString,
EventLogEntryType.Error)
结束尝试

''检查fileStream是否支持搜索
如果oWatcherFileSystemStream.CanSeek = False那么
oWatcherEventLogger.LogEvent(Me.EventLogName,"
filestream不支持Seeking& sCompleteFileNa我,EventLogEntryType.Error)
退出Sub
结束如果

''设置文件属性的EOF,因为我们要从这里处理所有的
更改。
lEofPosition = getCurrentEOF()
doWatchFileForChanges()
结束子

私函数getStreamsCurrentEOF()As Long
getStreamsCurrentEOF = CLng(oWatcherFileSystemStream .Length)

结束函数

公共函数doWatchFileForChanges()As Boolean
Dim oWatcherFileWatcher As New FileSystemWatcher
''设置我们要观察的路径
oWatcherFileWatcher.Path = sFilePath
''设置过滤器只观看一个文件
oWatcherFileWatcher.Filter = sFileName
''注意增加大小或更改lastwrittenflag oWatcherFileWatcher.NotifyFilter = NotifyFilters.LastWrite或
NotifyFilters.Size
''为我们想要处理的事件设置处理程序
AddHandler oWatcherFileWatcher.Changed,AddressOf
doWorkOnChangedFile
AddHandler oWatcherFileWatcher.Deleted,AddressOf
doWorkOnChangedFile

''告诉FileWatcher开始工作并举起活动
oWatcherFileWatcher.EnableRaisingEvents = True

结束功能
Private Sub doWorkOnChangedFile(ByVal source As Object,ByVal e As
System.IO.FileSystemEventArgs)
Select Case e.ChangeType
Case WatcherChangeTypes.Changed
Console.Write(doReadChangesInFile)
Case WatcherChangeTypes.Deleted
控制台.Write(文件已被删除)
结束选择
结束子
私有子doNotifyThatFileIsNoMore(ByVal源为对象,ByVal $ b $ as as

System.Io.FileSyst emEventArgs)

End Sub

私函数doReadChangesInFile()As String
如果Me.getCurrentEOF = Me.getLastKnownEOF那么
oWatcherEventLogger.LogEvent(我。 EventLogName,A'已更改


文件

事件''已触发,我正在尝试查找更改但文件大小为



与上次我做的相同 &安培; ,EventLogEntryType.Error)
返回" ''没有数据返回
结束如果
Dim iNumberOfBytes作为整数
''获取我们应该读取的字节数
iNumberOfBytes = Me.getCurrentEOF - Me.getLastKnownEOF < br'>''准备缓冲区以正确的大小读取数据
Dim abReaderBuffer(iNumberOfBytes)As Byte
''转到最后一次更改之前的最后一次EOF
oWatcherFileSystemStream.Seek(Me .getLastKnownEOF,


SeekOrigin.Begin)

''将数据读入我们的缓冲区
oWatcherFileSystemStream.Read(abReaderBuffer,0,
iNumberOfBytes)' '设置下一个已知的因为我们处理了已更改的数据
lEofPosition = getCurrentEOF()
返回Encoding.ASCII.GetString(abReaderBuffer)
结束函数




该死的...是跳为了NT,FSW无法观看...所以让我们实际上看看你的代码是
到yoru代码。 =)


" Kai Thorsrud" < KA ****** @ thispartgoesaways.start.no>在消息中写道

新闻:OD ************** @ TK2MSFTNGP09.phx.gbl ...

Windows XP专业版。它应该在完成后在Windows 2003服务器上运行

CJ Taylor <无**** @ blowgoats.com>在消息中写道
新闻:10 ************* @ corp.supernews.com ...

最重要的是..你是什么样的系统要继续运行吗?

Kai Thorsrud < KA ****** @ thispartgoesaways.start.no>在消息中写道
新闻:u9 ************** @ TK2MSFTNGP09.phx.gbl ...

你好我是一个活动Noob和我正在从我的主模块中调用一个类
console

app

这样。很抱歉,如果这是很多代码要阅读,但根据我的书,我看不到错误。
非常感谢。

如果这里还有其他的新手像我一样,如果有任何用途,我可以随意使用我的代码:)

我在doWorkOnChangedFile上设置了一个断点,但是没有发生......我是
洪水我的日志就像地狱一样。

Dim oFileWatcher As New sysLogIoHandler
oFileWatcher.FileNamePathWithOutEndingSlash =" C:\Program
Files \Syslogd\Logs"
oFileWatcher .FileName =" SyslogCatchAll.txt"
oFileWatcher.Constructor()
Do While 1< 2
System.Threading.Thread.Sleep(400)
循环

这是我班上的一些代码。我现在调用Sub Constructor


进行调试。

''---------------------- -------------------
类sysLogIoHandler代码
''------------------ -----------------------

Public Sub Constructor()

Dim oFileStream As System.IO .File
''确保文件支持以后使用的属性

然后设置文件的EOF位置
''检查文件是否存在
sCompleteFileName = sFilePath& " \" &安培; sFileName

如果File.Exists(sCompleteFileName)= False那么
oWatcherEventLogger.LogEvent(Me.EventLogName," File does


不存在或无权访问该文件:"& sFileName,
EventLogEntryType.Error)退出Sub
结束如果

''尝试打开FileStream
尝试
oWatcherFileSystemStream =


(oFileStream.Open(sCompleteFileName,

FileMode.Open,FileAccess.Read,FileShare.None))
Catch ex As Exception
oWatcherEventLogger.LogEvent(Me.EventLogName,
ex.ToString,EventLogEntryType.Error)
结束尝试

''检查fileStream是否支持搜索
如果oWatcherFileSystemStream .CanSeek = False然后
oWatcherEventLogger.LogEvent(Me.EventLogName,文件流不支持寻找& amp; sCompleteFileName,
EventLogEntryType.Error)退出Sub
结束如果

''设置文件属性的EOF,因为我们要从这里处理所有
更改on。
lEofPosition = getCurrentEOF()
doWatchFileForChanges()

$ $
私有函数getStreamsCurrentEOF()As get
getStreamsCurrentEOF = CLng (oWatcherFileSystemStream.Length)

结束功能


这是。


你声明你的FileWatcher错了上下文。不要在一个方法中声明它的类型

,但是在类中声明它


以便让你更容易将它声明为WithEvents,所以你有


Public WithEvents oWatcher as FileSystemWatch


然后在你的方法中


oWatcher = new FilesystemWatcher ()

公共函数doWatchFileForChanges()As Boolean
Dim oWatcherFileWatcher As New FileSystemWatcher
''设置我们要观察的路径
oWatcherFileWatcher.Path = sFilePath
''设置过滤器只看一个文件
oWatcherFileWatcher.Filter = sFileName
''注意增加大小或更改lastwrittenflag
oWatcherFileWatcher.NotifyFilter = NotifyFilters.LastWrite或
NotifyFilters.Size
''为我们想要处理的事件设置处理程序
AddHandler oWatcherFileWatcher.Changed,AddressOf
d oWorkOnChangedFile
AddHandler oWatcherFileWatcher.Deleted,AddressOf
doWorkOnChangedFile
告诉FileWatcher开始工作和举办活动
oWatcherFileWatcher.EnableRaisingEvents = True
Private Sub doWorkOnChangedFile(ByVal source As Object,ByVal e
As System.IO.FileSystemEventArgs)
Select Case e.ChangeType
Case WatcherChangeTypes.Changed Console.Write(doReadChangesInFile)
Case WatcherChangeTypes.Deleted
Console.Write(文件被删除)
结束选择
End Sub
Private Sub doNotifyThatFileIsNoMore(ByVal source As Object,
ByVal e

As

System.Io.FileSystemEventArgs)

End Sub

私有函数doReadChangesInFile()As String
如果Me.getCurrentEOF = Me.getLastKnownEOF则
oWatcherEventLogger.LogEvent(Me.EventLogName,A''已更改


文件

事件''已触发且我我试图找到变化,但文件大小与上次我做的相同 &安培; ,EventLogEntryType.Error)
返回" ''没有数据返回
结束如果
Dim iNumberOfBytes作为整数
''获取我们应该读取的字节数
iNumberOfBytes = Me.getCurrentEOF - Me.getLastKnownEOF < br'>''准备缓冲区以正确的大小读取数据
Dim abReaderBuffer(iNumberOfBytes)As Byte
''转到最后一次更改之前的最后一次EOF
oWatcherFileSystemStream.Seek(Me .getLastKnownEOF,


SeekOrigin.Begin)

''将数据读入我们的缓冲区
oWatcherFileSystemStream.Read(abReaderBuffer,0,iNumberOfBytes)''设置下一个knownEOF,因为我们已经处理了更改的



数据lEofPosition = getCurrentEOF()
返回Encoding.ASCII.GetString(abReaderBuffer)
结束功能





Hi i''m an event Noob and i''m calling a class from my main module console app
like this. I''m sorry if this is a lot of code to read but i can''t see the
error according to my book.
Thanks a LOT.

if there are other noobs here like me feel free to use my code if it''s of
any use :)

I''ve set a breakpoint at doWorkOnChangedFile but notinh happens... i''m
flooding my log like hell.
Dim oFileWatcher As New sysLogIoHandler
oFileWatcher.FileNamePathWithOutEndingSlash = "C:\Program
Files\Syslogd\Logs"
oFileWatcher.FileName = "SyslogCatchAll.txt"
oFileWatcher.Constructor()
Do While 1 < 2
System.Threading.Thread.Sleep(400)
Loop
And here is some code from my class. I call the Sub Constructor now for
debugging purposes.

''-----------------------------------------
Class sysLogIoHandler code
''-----------------------------------------

Public Sub Constructor()

Dim oFileStream As System.IO.File
'' Ensure that the file supports the properties we are to use later
on and then set the EOF position of the File
'' Check if the file exists
sCompleteFileName = sFilePath & "\" & sFileName

If File.Exists(sCompleteFileName) = False Then
oWatcherEventLogger.LogEvent(Me.EventLogName, "File does not
exists or no access to the file :" & sFileName, EventLogEntryType.Error)
Exit Sub
End If

'' Try to open a FileStream
Try
oWatcherFileSystemStream = (oFileStream.Open(sCompleteFileName,
FileMode.Open, FileAccess.Read, FileShare.None))
Catch ex As Exception
oWatcherEventLogger.LogEvent(Me.EventLogName, ex.ToString,
EventLogEntryType.Error)
End Try

'' Check if the fileStream supports seeking
If oWatcherFileSystemStream.CanSeek = False Then
oWatcherEventLogger.LogEvent(Me.EventLogName, "The filestream
does not support Seeking" & sCompleteFileName, EventLogEntryType.Error)
Exit Sub
End If

''Set the EOF of the file property since we are to handle all
changes from here on.
lEofPosition = getCurrentEOF()
doWatchFileForChanges()

End Sub
Private Function getStreamsCurrentEOF() As Long
getStreamsCurrentEOF = CLng(oWatcherFileSystemStream.Length)

End Function

Public Function doWatchFileForChanges() As Boolean
Dim oWatcherFileWatcher As New FileSystemWatcher
''Set the path we are to watch
oWatcherFileWatcher.Path = sFilePath
''Set the Filter to just watch one file
oWatcherFileWatcher.Filter = sFileName
''Watch for increase in size or change of lastwrittenflag
oWatcherFileWatcher.NotifyFilter = NotifyFilters.LastWrite Or
NotifyFilters.Size
''Set a handler to the events we want to handle
AddHandler oWatcherFileWatcher.Changed, AddressOf
doWorkOnChangedFile
AddHandler oWatcherFileWatcher.Deleted, AddressOf
doWorkOnChangedFile

'' Tell the FileWatcher to start working and raise events
oWatcherFileWatcher.EnableRaisingEvents = True

End Function
Private Sub doWorkOnChangedFile(ByVal source As Object, ByVal e As
System.IO.FileSystemEventArgs)
Select Case e.ChangeType
Case WatcherChangeTypes.Changed
Console.Write(doReadChangesInFile)
Case WatcherChangeTypes.Deleted
Console.Write("The file was deleted")
End Select
End Sub

Private Sub doNotifyThatFileIsNoMore(ByVal source As Object, ByVal e As
System.Io.FileSystemEventArgs)

End Sub

Private Function doReadChangesInFile() As String
If Me.getCurrentEOF = Me.getLastKnownEOF Then
oWatcherEventLogger.LogEvent(Me.EventLogName, "A ''Changed File
Event'' Triggered and I''m trying to find the change but the filesize is the
same as lasttime i did this" & , EventLogEntryType.Error)
Return "" '' Return with no Data
End If
Dim iNumberOfBytes As Integer
'' Get the number of bytes we are supposed to read
iNumberOfBytes = Me.getCurrentEOF - Me.getLastKnownEOF
'' Prepare Buffer to read data into with the correct size
Dim abReaderBuffer(iNumberOfBytes) As Byte
'' GoTo the last known EOF before the last change
oWatcherFileSystemStream.Seek(Me.getLastKnownEOF, SeekOrigin.Begin)
''Read the data into our buffer
oWatcherFileSystemStream.Read(abReaderBuffer, 0, iNumberOfBytes)
'' set the next knownEOF since we have handled the changed data
lEofPosition = getCurrentEOF()
Return Encoding.ASCII.GetString(abReaderBuffer)
End Function

解决方案

most importantly.. what kind of system are you tring to run it on?

"Kai Thorsrud" <ka******@thispartgoesaways.start.no> wrote in message
news:u9**************@TK2MSFTNGP09.phx.gbl...

Hi i''m an event Noob and i''m calling a class from my main module console app like this. I''m sorry if this is a lot of code to read but i can''t see the
error according to my book.
Thanks a LOT.

if there are other noobs here like me feel free to use my code if it''s of
any use :)

I''ve set a breakpoint at doWorkOnChangedFile but notinh happens... i''m
flooding my log like hell.
Dim oFileWatcher As New sysLogIoHandler
oFileWatcher.FileNamePathWithOutEndingSlash = "C:\Program
Files\Syslogd\Logs"
oFileWatcher.FileName = "SyslogCatchAll.txt"
oFileWatcher.Constructor()
Do While 1 < 2
System.Threading.Thread.Sleep(400)
Loop
And here is some code from my class. I call the Sub Constructor now for
debugging purposes.

''-----------------------------------------
Class sysLogIoHandler code
''-----------------------------------------

Public Sub Constructor()

Dim oFileStream As System.IO.File
'' Ensure that the file supports the properties we are to use later on and then set the EOF position of the File
'' Check if the file exists
sCompleteFileName = sFilePath & "\" & sFileName

If File.Exists(sCompleteFileName) = False Then
oWatcherEventLogger.LogEvent(Me.EventLogName, "File does not
exists or no access to the file :" & sFileName, EventLogEntryType.Error)
Exit Sub
End If

'' Try to open a FileStream
Try
oWatcherFileSystemStream = (oFileStream.Open(sCompleteFileName, FileMode.Open, FileAccess.Read, FileShare.None))
Catch ex As Exception
oWatcherEventLogger.LogEvent(Me.EventLogName, ex.ToString,
EventLogEntryType.Error)
End Try

'' Check if the fileStream supports seeking
If oWatcherFileSystemStream.CanSeek = False Then
oWatcherEventLogger.LogEvent(Me.EventLogName, "The filestream
does not support Seeking" & sCompleteFileName, EventLogEntryType.Error)
Exit Sub
End If

''Set the EOF of the file property since we are to handle all
changes from here on.
lEofPosition = getCurrentEOF()
doWatchFileForChanges()

End Sub
Private Function getStreamsCurrentEOF() As Long
getStreamsCurrentEOF = CLng(oWatcherFileSystemStream.Length)

End Function

Public Function doWatchFileForChanges() As Boolean
Dim oWatcherFileWatcher As New FileSystemWatcher
''Set the path we are to watch
oWatcherFileWatcher.Path = sFilePath
''Set the Filter to just watch one file
oWatcherFileWatcher.Filter = sFileName
''Watch for increase in size or change of lastwrittenflag
oWatcherFileWatcher.NotifyFilter = NotifyFilters.LastWrite Or
NotifyFilters.Size
''Set a handler to the events we want to handle
AddHandler oWatcherFileWatcher.Changed, AddressOf
doWorkOnChangedFile
AddHandler oWatcherFileWatcher.Deleted, AddressOf
doWorkOnChangedFile

'' Tell the FileWatcher to start working and raise events
oWatcherFileWatcher.EnableRaisingEvents = True

End Function
Private Sub doWorkOnChangedFile(ByVal source As Object, ByVal e As
System.IO.FileSystemEventArgs)
Select Case e.ChangeType
Case WatcherChangeTypes.Changed
Console.Write(doReadChangesInFile)
Case WatcherChangeTypes.Deleted
Console.Write("The file was deleted")
End Select
End Sub

Private Sub doNotifyThatFileIsNoMore(ByVal source As Object, ByVal e As System.Io.FileSystemEventArgs)

End Sub

Private Function doReadChangesInFile() As String
If Me.getCurrentEOF = Me.getLastKnownEOF Then
oWatcherEventLogger.LogEvent(Me.EventLogName, "A ''Changed File Event'' Triggered and I''m trying to find the change but the filesize is the
same as lasttime i did this" & , EventLogEntryType.Error)
Return "" '' Return with no Data
End If
Dim iNumberOfBytes As Integer
'' Get the number of bytes we are supposed to read
iNumberOfBytes = Me.getCurrentEOF - Me.getLastKnownEOF
'' Prepare Buffer to read data into with the correct size
Dim abReaderBuffer(iNumberOfBytes) As Byte
'' GoTo the last known EOF before the last change
oWatcherFileSystemStream.Seek(Me.getLastKnownEOF, SeekOrigin.Begin) ''Read the data into our buffer
oWatcherFileSystemStream.Read(abReaderBuffer, 0, iNumberOfBytes)
'' set the next knownEOF since we have handled the changed data
lEofPosition = getCurrentEOF()
Return Encoding.ASCII.GetString(abReaderBuffer)
End Function



Windows XP Pro. It''s supposed to run on Windows 2003 server when done
"CJ Taylor" <no****@blowgoats.com> wrote in message
news:10*************@corp.supernews.com...

most importantly.. what kind of system are you tring to run it on?

"Kai Thorsrud" <ka******@thispartgoesaways.start.no> wrote in message
news:u9**************@TK2MSFTNGP09.phx.gbl...

Hi i''m an event Noob and i''m calling a class from my main module console app

like this. I''m sorry if this is a lot of code to read but i can''t see the error according to my book.
Thanks a LOT.

if there are other noobs here like me feel free to use my code if it''s of any use :)

I''ve set a breakpoint at doWorkOnChangedFile but notinh happens... i''m
flooding my log like hell.
Dim oFileWatcher As New sysLogIoHandler
oFileWatcher.FileNamePathWithOutEndingSlash = "C:\Program
Files\Syslogd\Logs"
oFileWatcher.FileName = "SyslogCatchAll.txt"
oFileWatcher.Constructor()
Do While 1 < 2
System.Threading.Thread.Sleep(400)
Loop
And here is some code from my class. I call the Sub Constructor now for
debugging purposes.

''-----------------------------------------
Class sysLogIoHandler code
''-----------------------------------------

Public Sub Constructor()

Dim oFileStream As System.IO.File
'' Ensure that the file supports the properties we are to use


later

on and then set the EOF position of the File
'' Check if the file exists
sCompleteFileName = sFilePath & "\" & sFileName

If File.Exists(sCompleteFileName) = False Then
oWatcherEventLogger.LogEvent(Me.EventLogName, "File does not exists or no access to the file :" & sFileName, EventLogEntryType.Error)
Exit Sub
End If

'' Try to open a FileStream
Try
oWatcherFileSystemStream =


(oFileStream.Open(sCompleteFileName,

FileMode.Open, FileAccess.Read, FileShare.None))
Catch ex As Exception
oWatcherEventLogger.LogEvent(Me.EventLogName, ex.ToString,
EventLogEntryType.Error)
End Try

'' Check if the fileStream supports seeking
If oWatcherFileSystemStream.CanSeek = False Then
oWatcherEventLogger.LogEvent(Me.EventLogName, "The filestream does not support Seeking" & sCompleteFileName, EventLogEntryType.Error)
Exit Sub
End If

''Set the EOF of the file property since we are to handle all
changes from here on.
lEofPosition = getCurrentEOF()
doWatchFileForChanges()

End Sub
Private Function getStreamsCurrentEOF() As Long
getStreamsCurrentEOF = CLng(oWatcherFileSystemStream.Length)

End Function

Public Function doWatchFileForChanges() As Boolean
Dim oWatcherFileWatcher As New FileSystemWatcher
''Set the path we are to watch
oWatcherFileWatcher.Path = sFilePath
''Set the Filter to just watch one file
oWatcherFileWatcher.Filter = sFileName
''Watch for increase in size or change of lastwrittenflag
oWatcherFileWatcher.NotifyFilter = NotifyFilters.LastWrite Or
NotifyFilters.Size
''Set a handler to the events we want to handle
AddHandler oWatcherFileWatcher.Changed, AddressOf
doWorkOnChangedFile
AddHandler oWatcherFileWatcher.Deleted, AddressOf
doWorkOnChangedFile

'' Tell the FileWatcher to start working and raise events
oWatcherFileWatcher.EnableRaisingEvents = True

End Function
Private Sub doWorkOnChangedFile(ByVal source As Object, ByVal e As
System.IO.FileSystemEventArgs)
Select Case e.ChangeType
Case WatcherChangeTypes.Changed
Console.Write(doReadChangesInFile)
Case WatcherChangeTypes.Deleted
Console.Write("The file was deleted")
End Select
End Sub

Private Sub doNotifyThatFileIsNoMore(ByVal source As Object, ByVal e As

System.Io.FileSystemEventArgs)

End Sub

Private Function doReadChangesInFile() As String
If Me.getCurrentEOF = Me.getLastKnownEOF Then
oWatcherEventLogger.LogEvent(Me.EventLogName, "A ''Changed


File

Event'' Triggered and I''m trying to find the change but the filesize is


the same as lasttime i did this" & , EventLogEntryType.Error)
Return "" '' Return with no Data
End If
Dim iNumberOfBytes As Integer
'' Get the number of bytes we are supposed to read
iNumberOfBytes = Me.getCurrentEOF - Me.getLastKnownEOF
'' Prepare Buffer to read data into with the correct size
Dim abReaderBuffer(iNumberOfBytes) As Byte
'' GoTo the last known EOF before the last change
oWatcherFileSystemStream.Seek(Me.getLastKnownEOF,


SeekOrigin.Begin)

''Read the data into our buffer
oWatcherFileSystemStream.Read(abReaderBuffer, 0, iNumberOfBytes) '' set the next knownEOF since we have handled the changed data
lEofPosition = getCurrentEOF()
Return Encoding.ASCII.GetString(abReaderBuffer)
End Function




damn.. was hoping for NT, which FSW can''t watch on... so lets actually look
into yoru code. =)

"Kai Thorsrud" <ka******@thispartgoesaways.start.no> wrote in message
news:OD**************@TK2MSFTNGP09.phx.gbl...

Windows XP Pro. It''s supposed to run on Windows 2003 server when done
"CJ Taylor" <no****@blowgoats.com> wrote in message
news:10*************@corp.supernews.com...

most importantly.. what kind of system are you tring to run it on?

"Kai Thorsrud" <ka******@thispartgoesaways.start.no> wrote in message
news:u9**************@TK2MSFTNGP09.phx.gbl...

Hi i''m an event Noob and i''m calling a class from my main module console

app

like this. I''m sorry if this is a lot of code to read but i can''t see the error according to my book.
Thanks a LOT.

if there are other noobs here like me feel free to use my code if it''s of any use :)

I''ve set a breakpoint at doWorkOnChangedFile but notinh happens... i''m
flooding my log like hell.
Dim oFileWatcher As New sysLogIoHandler
oFileWatcher.FileNamePathWithOutEndingSlash = "C:\Program
Files\Syslogd\Logs"
oFileWatcher.FileName = "SyslogCatchAll.txt"
oFileWatcher.Constructor()
Do While 1 < 2
System.Threading.Thread.Sleep(400)
Loop
And here is some code from my class. I call the Sub Constructor now

for debugging purposes.

''-----------------------------------------
Class sysLogIoHandler code
''-----------------------------------------

Public Sub Constructor()

Dim oFileStream As System.IO.File
'' Ensure that the file supports the properties we are to use later

on and then set the EOF position of the File
'' Check if the file exists
sCompleteFileName = sFilePath & "\" & sFileName

If File.Exists(sCompleteFileName) = False Then
oWatcherEventLogger.LogEvent(Me.EventLogName, "File does


not exists or no access to the file :" & sFileName, EventLogEntryType.Error) Exit Sub
End If

'' Try to open a FileStream
Try
oWatcherFileSystemStream =


(oFileStream.Open(sCompleteFileName,

FileMode.Open, FileAccess.Read, FileShare.None))
Catch ex As Exception
oWatcherEventLogger.LogEvent(Me.EventLogName, ex.ToString, EventLogEntryType.Error)
End Try

'' Check if the fileStream supports seeking
If oWatcherFileSystemStream.CanSeek = False Then
oWatcherEventLogger.LogEvent(Me.EventLogName, "The filestream does not support Seeking" & sCompleteFileName, EventLogEntryType.Error) Exit Sub
End If

''Set the EOF of the file property since we are to handle all
changes from here on.
lEofPosition = getCurrentEOF()
doWatchFileForChanges()

End Sub
Private Function getStreamsCurrentEOF() As Long
getStreamsCurrentEOF = CLng(oWatcherFileSystemStream.Length)

End Function

HERE IT IS.

you declare your FileWatcher in the wrong context. Don''t declare its type
within a method, but declare it within the class

to make things easier you would declare it WithEvents, so you have

Public WithEvents oWatcher as FileSystemWatch

then in your method below

oWatcher = new FilesystemWatcher()
Public Function doWatchFileForChanges() As Boolean
Dim oWatcherFileWatcher As New FileSystemWatcher
''Set the path we are to watch
oWatcherFileWatcher.Path = sFilePath
''Set the Filter to just watch one file
oWatcherFileWatcher.Filter = sFileName
''Watch for increase in size or change of lastwrittenflag
oWatcherFileWatcher.NotifyFilter = NotifyFilters.LastWrite Or
NotifyFilters.Size
''Set a handler to the events we want to handle
AddHandler oWatcherFileWatcher.Changed, AddressOf
doWorkOnChangedFile
AddHandler oWatcherFileWatcher.Deleted, AddressOf
doWorkOnChangedFile

'' Tell the FileWatcher to start working and raise events
oWatcherFileWatcher.EnableRaisingEvents = True

End Function
Private Sub doWorkOnChangedFile(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)
Select Case e.ChangeType
Case WatcherChangeTypes.Changed
Console.Write(doReadChangesInFile)
Case WatcherChangeTypes.Deleted
Console.Write("The file was deleted")
End Select
End Sub

Private Sub doNotifyThatFileIsNoMore(ByVal source As Object, ByVal e

As

System.Io.FileSystemEventArgs)

End Sub

Private Function doReadChangesInFile() As String
If Me.getCurrentEOF = Me.getLastKnownEOF Then
oWatcherEventLogger.LogEvent(Me.EventLogName, "A ''Changed


File

Event'' Triggered and I''m trying to find the change but the filesize is the same as lasttime i did this" & , EventLogEntryType.Error)
Return "" '' Return with no Data
End If
Dim iNumberOfBytes As Integer
'' Get the number of bytes we are supposed to read
iNumberOfBytes = Me.getCurrentEOF - Me.getLastKnownEOF
'' Prepare Buffer to read data into with the correct size
Dim abReaderBuffer(iNumberOfBytes) As Byte
'' GoTo the last known EOF before the last change
oWatcherFileSystemStream.Seek(Me.getLastKnownEOF,


SeekOrigin.Begin)

''Read the data into our buffer
oWatcherFileSystemStream.Read(abReaderBuffer, 0, iNumberOfBytes) '' set the next knownEOF since we have handled the changed


data lEofPosition = getCurrentEOF()
Return Encoding.ASCII.GetString(abReaderBuffer)
End Function





这篇关于为什么我的FileSystemWatcher事件没有激活?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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