By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,552 Members | 905 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,552 IT Pros & Developers. It's quick & easy.

Why isn't my FileSystemWatcher events fireing ?

P: n/a
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

Nov 20 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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

Nov 20 '05 #2

P: n/a
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


Nov 20 '05 #3

P: n/a
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



Nov 20 '05 #4

P: n/a
Whoops, forogto to tell you my comment for your fix is inline...(scroll down
in other message)

see previous post.

"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

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



Nov 20 '05 #5

P: n/a
Thanks for your help CJ (Are you the same CJ that used to hang on
#windowsnt on irc back in 1997 ?)

Beaming a beer to you in return (damn... core dump... hmm damn debug)

However it still doesn't raise events... reading like hell over here...
I also tried adding this line after EnableRaisingEvents

oWatcherFileWatcher.EnableRaisingEvents = True
oWatcherFileWatcher.WaitForChanged(WatcherChangeTy pes.All)

but WaitForChanged never returns...

Nov 20 '05 #6

P: n/a
Are you using an actual event handler? I mean this is in the nicest way
possible, but do you understand event driven programming?

WaitForChanged only works on the last fired event. it doesn't actually
notify. It kinda works like a timer.

what you want to do is have aother method in there that is like this
WaitForChanged is basically
"Kai Thorsrud" <ka*********************@Start.no> wrote in message
news:Xn**********************************@207.46.2 48.16...
Thanks for your help CJ (Are you the same CJ that used to hang on
#windowsnt on irc back in 1997 ?)

Beaming a beer to you in return (damn... core dump... hmm damn debug)

However it still doesn't raise events... reading like hell over here...
I also tried adding this line after EnableRaisingEvents

oWatcherFileWatcher.EnableRaisingEvents = True
oWatcherFileWatcher.WaitForChanged(WatcherChangeTy pes.All)

but WaitForChanged never returns...


Private Shared Sub OnChanged(source As Object, e As FileSystemEventArgs) _
handles oWatcherFileWatcher.Created,
oWatcherFileWatcher.Changed, oWatcherFileWatch.Deleted, _
oWatcherFileWatcher.Renamed
' Specify what is done when a file is changed, created, or deleted.
Console.WriteLine("File: " & e.FullPath & " " & e.ChangeType)
End Sub

put a breakpoint on that one to see when the file watcher changes...
Asyncronous programming =)
Nov 20 '05 #7

P: n/a
Ahh yes, and as for windowsnt, yeah I did sometimes, but I remember a lot of
people did... a lot of people are named CJ too . =)
"Kai Thorsrud" <ka*********************@Start.no> wrote in message
news:Xn**********************************@207.46.2 48.16...
Thanks for your help CJ (Are you the same CJ that used to hang on
#windowsnt on irc back in 1997 ?)

Beaming a beer to you in return (damn... core dump... hmm damn debug)

However it still doesn't raise events... reading like hell over here...
I also tried adding this line after EnableRaisingEvents

oWatcherFileWatcher.EnableRaisingEvents = True
oWatcherFileWatcher.WaitForChanged(WatcherChangeTy pes.All)

but WaitForChanged never returns...

Nov 20 '05 #8

P: n/a
haha you really made my day.... Thanks a lot CJ.... are you the same CJ that
used to hang out on irc #windowsNT supporting ppl ? (I used to hang out
there a lot back in 1997 i don't remember my nick thou)

Beaming a cold beer right back to you (damn core dump... oh well in a few
years maybe)

/Kai

"CJ Taylor" <no****@blowgoats.com> wrote in message
news:10*************@corp.supernews.com...
Whoops, forogto to tell you my comment for your fix is inline...(scroll down in other message)

see previous post.

"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
>
> 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
>
>
>




Nov 20 '05 #9

P: n/a
Hi again!

I just came home from work and i've been playing around with the code for a
while... It turns out there is nothing wrong with my code...
I changed the code to monitor the entire directory and it triggers if i have
a text file in the directory and i open the text file in textpad and change
that file....

However it wont trigger on my Kiwi Syslog Deamon's log file ( The
SyslogDeamon is running and windows explorer updates the logfile's
attributes everytime the logfile get's updated).
The logfile has a lock on it. i can open the logfile in textpad but i can't
write to it since there is a write lock on it...

Is this a limitation of the FileSystemWatcher ? If so is there a way around
this ? Darn fck'in stupid FileSystemWatcher if it is so.... The windows
Explorer sure notify the changes in the file and update's it's size and file
attributes...

/Kai

"Kai Thorsrud" <ka*********************@Start.no> wrote in message
news:Xn**********************************@207.46.2 48.16...
Thanks for your help CJ (Are you the same CJ that used to hang on
#windowsnt on irc back in 1997 ?)

Beaming a beer to you in return (damn... core dump... hmm damn debug)

However it still doesn't raise events... reading like hell over here...
I also tried adding this line after EnableRaisingEvents

oWatcherFileWatcher.EnableRaisingEvents = True
oWatcherFileWatcher.WaitForChanged(WatcherChangeTy pes.All)

but WaitForChanged never returns...

Nov 20 '05 #10

P: n/a
damn stupid me... i have solved it... in my contructor i also open this log
file for reading to have a stream ready for the event which is supposed to
read the changes in the log file :D


"Kai Thorsrud" <ka******@thispartgoesaways.start.no> wrote in message
news:en**************@TK2MSFTNGP11.phx.gbl...
Hi again!

I just came home from work and i've been playing around with the code for a while... It turns out there is nothing wrong with my code...
I changed the code to monitor the entire directory and it triggers if i have a text file in the directory and i open the text file in textpad and change that file....

However it wont trigger on my Kiwi Syslog Deamon's log file ( The
SyslogDeamon is running and windows explorer updates the logfile's
attributes everytime the logfile get's updated).
The logfile has a lock on it. i can open the logfile in textpad but i can't write to it since there is a write lock on it...

Is this a limitation of the FileSystemWatcher ? If so is there a way around this ? Darn fck'in stupid FileSystemWatcher if it is so.... The windows
Explorer sure notify the changes in the file and update's it's size and file attributes...

/Kai

"Kai Thorsrud" <ka*********************@Start.no> wrote in message
news:Xn**********************************@207.46.2 48.16...
Thanks for your help CJ (Are you the same CJ that used to hang on
#windowsnt on irc back in 1997 ?)

Beaming a beer to you in return (damn... core dump... hmm damn debug)

However it still doesn't raise events... reading like hell over here...
I also tried adding this line after EnableRaisingEvents

oWatcherFileWatcher.EnableRaisingEvents = True
oWatcherFileWatcher.WaitForChanged(WatcherChangeTy pes.All)

but WaitForChanged never returns...


Nov 20 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.