I'm working on a Windows Service that watches a few folders for changes, creations, and deletions. It all works well with the exception of one watcher that watches a single file (XML File with Configuration Settings) for minor changes.
I tried taking the Windows Service code and putting it into a simple windows application with start/stop buttons for the filesystem watchers and stepped through it. It never detects the file change of the XML config file. The changes are indeed occurring and the "Date Modified" of the file is updating.
XmlEventReferences = New System.IO.FileSystemWatcher()
XmlEventReferences.Path = "C:\XmlReferences\"
XmlEventReferences.Filter = "*.xml"
XmlEventReferences.NotifyFilter = IO.NotifyFilters.FileName
AddHandler XmlEventReferences.Changed, AddressOf ReloadEventReferences
AddHandler XmlEventReferences.Created, AddressOf ReloadEventReferences
AddHandler XmlEventReferences., AddressOf ReloadEventReferences
XmlEventReferences.EnableRaisingEvents = True
That was some of the code and this is a sample of the XML File:
<EventReference>
<ER_EL_NUMBER>1</ER_EL_NUMBER>
<ER_SEND_TO_DATABASE>true</ER_SEND_TO_DATABASE>
<ER_ACTIVATE_ALARM>true</ER_ACTIVATE_ALARM>
<ER_DESCRIPTION />
</EventReference>
-
You need to change your
.NotifyFilterto something likeLastWriteto pick up changes i believe.MSDN link here
-
I believe the problem is the value of
NotifyFilter. You've actually only told theFileSystemWatcherto look for file name changes. To get it to raise theChangedevent for file modifications too, you'll want to specify theLastWriteflag as well.i.e. The appropiate line of code should be changed to:
XmlEventReferences.NotifyFilter = IO.NotifyFilters.FileName | IO.NotifyFilters.LastWrite;See the
NotifyFilterspage on MSDN for more info.Note: As Joshua Belden points out, you don't even need to set the
NotifyFilterproperty at all because as MSDN states: "The default is the bitwise OR combination of LastWrite, FileName, and DirectoryName." However, I would argue that it's always best to be explicit in such cases - it then makes it perfectly obvious as to what theFileSystemWatcheris and is not watching.Paxenos : Thanks! I appreciate the help -
This code seemed to work for me, picked up an edit to a test.xml file. Are
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim XmlEventReferences = New System.IO.FileSystemWatcher() XmlEventReferences.Path = "C:\" XmlEventReferences.Filter = "*.xml" XmlEventReferences.EnableRaisingEvents = True AddHandler XmlEventReferences.Changed, AddressOf Watch End Sub Private Sub Watch(ByVal sender As Object, ByVal e As FileSystemEventArgs) Dim s As String = e.FullPath End SubDump the notify filter all together.
Noldorin : From MSDN (http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.notifyfilter.aspx): "The default is the bitwise OR combination of LastWrite, FileName, and DirectoryName." So yeah, you don't need the line specifying NotifyFilter, but it doesn't hurt to be explicit about what you are watchin for. (I would include it anyway for this reason.)
0 comments:
Post a Comment