{"id":4,"date":"2007-11-12T15:14:58","date_gmt":"2007-11-12T08:14:58","guid":{"rendered":"http:\/\/sg20.com\/techblog\/2007\/11\/12\/filesystemwatcher-doesnt-work-on-vista\/"},"modified":"2007-11-12T15:14:58","modified_gmt":"2007-11-12T08:14:58","slug":"filesystemwatcher-doesnt-work-on-vista","status":"publish","type":"post","link":"https:\/\/sg20.com\/techblog\/2007\/11\/12\/filesystemwatcher-doesnt-work-on-vista\/","title":{"rendered":"FileSystemWatcher doesn&#8217;t work on Vista"},"content":{"rendered":"<p>The <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/system.io.filesystemwatcher.aspx\">FileSystemWatcher<\/a> class <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/system.io.filesystemwatcher.aspx\"><\/a>is part of the .NET framework and is used to watch for changes in directories or files.\u00a0 For programs that need to monitor particular files, this class is perfect and very helpful.\u00a0 I wrote a program to parse a log file and used the FileSystemWatcher class to monitor the log file\u00a0 and raise an event when a new line is added to the log.\u00a0 My code worked great on Windows XP but after upgrading to Windows Vista I noticed that the FileSystemWatcher stopped working.<\/p>\n<p>The FileSystemWatcher seems to have a bug on Vista where\u00a0 if the file is currently open by another program (as in the case with real-time log files) then it will not notice any changes made to the file.\u00a0 From my own experience I found that if I used Windows Explorer to browse to the log folder (or refreshed the folder if Windows Explorer was already there) then the FileSystemWatcher would finally pick up the changes.\u00a0 I experienced this bug on .NET 2.0 so I&#8217;m not certain if it exists in .NET 1.1 or 3.0 on Vista.<\/p>\n<p>I eventually came up with a workaround for my particular program.\u00a0 You basically need to add\u00a0 your own timer which will check the file for modifications.\u00a0 You could create your own code to act as a FileSystemWatcher and look for new lines added.\u00a0 But this involves rewriting your code.\u00a0 Another solution is to use a <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/system.io.fileinfo.aspx\">FileInfo<\/a> object to help the FileSystemWatcher.\u00a0 In my program I created a timer that would fire once per second.\u00a0 The timer creates a FileInfo object for the same log file that the FileSystemWatcher is currently watching.\u00a0 It will then call the Refresh method of the FileInfo object.\u00a0 Calling the Refresh method will cause the FileSystemWatcher to notice any changes to the file and raise the proper event like it is supposed to.\u00a0 Since the timer code is so short there is no noticeable performance increase.\u00a0 This is the preferred workaround since you don&#8217;t need to modify any of your existing code.\u00a0 Additionally, whenever this bug is fixed you can just remove the timer and you will not have to rewrite any code.\u00a0 If your program also runs on other operating systems (like XP or 2003 server) then you could probably add some additional code to check which operating system is running and only use the timer code if it is running on Vista.<\/p>\n<p>I discussed this particular bug with others on the MSDN forums at: <a href=\"http:\/\/forums.microsoft.com\/MSDN\/ShowPost.aspx?PostID=1569013&amp;SiteID=1\">http:\/\/forums.microsoft.com\/MSDN\/ShowPost.aspx?PostID=1569013&amp;SiteID=1<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The FileSystemWatcher class is part of the .NET framework and is used to watch for changes in directories or files.\u00a0 For programs that need to monitor particular files, this class is perfect and very helpful.\u00a0 I wrote a program to parse a log file and used the FileSystemWatcher class to monitor the log file\u00a0 and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4,3],"tags":[],"_links":{"self":[{"href":"https:\/\/sg20.com\/techblog\/wp-json\/wp\/v2\/posts\/4"}],"collection":[{"href":"https:\/\/sg20.com\/techblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sg20.com\/techblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sg20.com\/techblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sg20.com\/techblog\/wp-json\/wp\/v2\/comments?post=4"}],"version-history":[{"count":0,"href":"https:\/\/sg20.com\/techblog\/wp-json\/wp\/v2\/posts\/4\/revisions"}],"wp:attachment":[{"href":"https:\/\/sg20.com\/techblog\/wp-json\/wp\/v2\/media?parent=4"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sg20.com\/techblog\/wp-json\/wp\/v2\/categories?post=4"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sg20.com\/techblog\/wp-json\/wp\/v2\/tags?post=4"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}