{"id":931,"date":"2009-11-27T10:38:54","date_gmt":"2009-11-27T09:38:54","guid":{"rendered":"http:\/\/www.navision-blog.de\/2009\/11\/27\/observing-the-filesystem\/"},"modified":"2009-11-27T10:41:38","modified_gmt":"2009-11-27T09:41:38","slug":"observing-the-filesystem","status":"publish","type":"post","link":"http:\/\/www.navision-blog.de\/blog\/2009\/11\/27\/observing-the-filesystem\/","title":{"rendered":"Observing the FileSystem"},"content":{"rendered":"<p>The following class is a wrapper for the System.IO.FileSystemWatcher and converts the FileSystem events into observables. You need to download and reference the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/devlabs\/ee794896.aspx\">Reactive Extensions for .NET (Rx)<\/a> to use this code:<\/p>\n<div style=\"font-family: courier new; background: white; color: black; font-size: 10pt\">\n<p style=\"margin: 0px\"><span style=\"color: blue\">public<\/span> <span style=\"color: blue\">class<\/span> <span style=\"color: #2b91af\">FileSystemObservable<\/span><\/p>\n<p style=\"margin: 0px\">{<\/p>\n<p style=\"margin: 0px\">&#160; <span style=\"color: blue\">private<\/span> <span style=\"color: blue\">readonly<\/span> <span style=\"color: #2b91af\">FileSystemWatcher<\/span> _fileSystemWatcher;<\/p>\n<p style=\"margin: 0px\">&#160;<\/p>\n<p style=\"margin: 0px\">&#160;<\/p>\n<p style=\"margin: 0px\">&#160; <span style=\"color: blue\">public<\/span> FileSystemObservable(<span style=\"color: blue\">string<\/span> directory, <\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160; <span style=\"color: blue\">string<\/span> filter, <span style=\"color: blue\">bool<\/span> includeSubdirectories)<\/p>\n<p style=\"margin: 0px\">&#160; {<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160; _fileSystemWatcher =<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160; <span style=\"color: blue\">new<\/span> <span style=\"color: #2b91af\">FileSystemWatcher<\/span>(directory, filter)<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; EnableRaisingEvents = <span style=\"color: blue\">true<\/span>,<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; IncludeSubdirectories = includeSubdirectories<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160;&#160;&#160; };<\/p>\n<p style=\"margin: 0px\">&#160;<\/p>\n<p style=\"margin: 0px\">&#160;<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160; ChangedFiles = <\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160; <span style=\"color: #2b91af\">Observable<\/span>.FromEvent&lt;<span style=\"color: #2b91af\">FileSystemEventHandler<\/span>, <span style=\"color: #2b91af\"><\/span><\/p>\n<p style=\"margin: 0px\"><span style=\"color: #2b91af\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; FileSystemEventArgs<\/span>&gt;<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160; (h =&gt; h.Invoke,<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160;&#160; h =&gt; _fileSystemWatcher.Changed += h,<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160;&#160; h =&gt; _fileSystemWatcher.Changed -= h)<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160; .Select(x =&gt; x.EventArgs);<\/p>\n<p style=\"margin: 0px\">&#160;<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160; CreatedFiles = <\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160; <span style=\"color: #2b91af\">Observable<\/span>.FromEvent&lt;<span style=\"color: #2b91af\">FileSystemEventHandler<\/span>,<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style=\"color: #2b91af\">FileSystemEventArgs<\/span>&gt;<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160; (h =&gt; h.Invoke,<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160;&#160; h =&gt; _fileSystemWatcher.Created += h,<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160;&#160; h =&gt; _fileSystemWatcher.Created -= h)<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160; .Select(x =&gt; x.EventArgs);<\/p>\n<p style=\"margin: 0px\">&#160;<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160; DeletedFiles = <\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160; <span style=\"color: #2b91af\">Observable<\/span>.FromEvent&lt;<span style=\"color: #2b91af\">FileSystemEventHandler<\/span>, <\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style=\"color: #2b91af\">FileSystemEventArgs<\/span>&gt;<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160; (h =&gt; h.Invoke,<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160;&#160; h =&gt; _fileSystemWatcher.Deleted += h,<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160;&#160; h =&gt; _fileSystemWatcher.Deleted -= h)<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160; .Select(x =&gt; x.EventArgs);<\/p>\n<p style=\"margin: 0px\">&#160;<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160; RenamedFiles = <\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160; <span style=\"color: #2b91af\">Observable<\/span>.FromEvent&lt;<span style=\"color: #2b91af\">RenamedEventHandler<\/span>, <span style=\"color: #2b91af\"><\/span><\/p>\n<p style=\"margin: 0px\"><span style=\"color: #2b91af\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; RenamedEventArgs<\/span>&gt;<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160; (h =&gt; h.Invoke,<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160;&#160; h =&gt; _fileSystemWatcher.Renamed += h,<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160;&#160; h =&gt; _fileSystemWatcher.Renamed -= h)<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160; .Select(x =&gt; x.EventArgs);<\/p>\n<p style=\"margin: 0px\">&#160;<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160; Errors = <\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160; <span style=\"color: #2b91af\">Observable<\/span>.FromEvent&lt;<span style=\"color: #2b91af\">ErrorEventHandler<\/span>, <span style=\"color: #2b91af\">ErrorEventArgs<\/span>&gt;<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160; (h =&gt; h.Invoke,<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160;&#160; h =&gt; _fileSystemWatcher.Error += h,<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160;&#160; h =&gt; _fileSystemWatcher.Error -= h)<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160; .Select(x =&gt; x.EventArgs);<\/p>\n<p style=\"margin: 0px\">&#160; }<\/p>\n<p style=\"margin: 0px\">&#160;<\/p>\n<p style=\"margin: 0px\">&#160; <span style=\"color: gray\">\/\/\/<\/span><span style=\"color: green\"> <\/span><span style=\"color: gray\">&lt;summary&gt;<\/span><\/p>\n<p style=\"margin: 0px\">&#160; <span style=\"color: gray\">\/\/\/<\/span><span style=\"color: green\"> Gets or sets the errors.<\/span><\/p>\n<p style=\"margin: 0px\">&#160; <span style=\"color: gray\">\/\/\/<\/span><span style=\"color: green\"> <\/span><span style=\"color: gray\">&lt;\/summary&gt;<\/span><\/p>\n<p style=\"margin: 0px\">&#160; <span style=\"color: gray\">\/\/\/<\/span><span style=\"color: green\"> <\/span><span style=\"color: gray\">&lt;value&gt;<\/span><span style=\"color: green\">The errors.<\/span><span style=\"color: gray\">&lt;\/value&gt;<\/span><\/p>\n<p style=\"margin: 0px\">&#160; <span style=\"color: blue\">public<\/span> <span style=\"color: #2b91af\">IObservable<\/span>&lt;<span style=\"color: #2b91af\">ErrorEventArgs<\/span>&gt; Errors <\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; { <span style=\"color: blue\">get<\/span>; <span style=\"color: blue\">private<\/span> <span style=\"color: blue\">set<\/span>; }<\/p>\n<p style=\"margin: 0px\">&#160;<\/p>\n<p style=\"margin: 0px\">&#160; <span style=\"color: gray\">\/\/\/<\/span><span style=\"color: green\"> <\/span><span style=\"color: gray\">&lt;summary&gt;<\/span><\/p>\n<p style=\"margin: 0px\">&#160; <span style=\"color: gray\">\/\/\/<\/span><span style=\"color: green\"> Gets the changed files.<\/span><\/p>\n<p style=\"margin: 0px\">&#160; <span style=\"color: gray\">\/\/\/<\/span><span style=\"color: green\"> <\/span><span style=\"color: gray\">&lt;\/summary&gt;<\/span><\/p>\n<p style=\"margin: 0px\">&#160; <span style=\"color: gray\">\/\/\/<\/span><span style=\"color: green\"> <\/span><span style=\"color: gray\">&lt;value&gt;<\/span><span style=\"color: green\">The changed files.<\/span><span style=\"color: gray\">&lt;\/value&gt;<\/span><\/p>\n<p style=\"margin: 0px\">&#160; <span style=\"color: blue\">public<\/span> <span style=\"color: #2b91af\">IObservable<\/span>&lt;<span style=\"color: #2b91af\">FileSystemEventArgs<\/span>&gt; ChangedFiles <\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; { <span style=\"color: blue\">get<\/span>; <span style=\"color: blue\">private<\/span> <span style=\"color: blue\">set<\/span>; }<\/p>\n<p style=\"margin: 0px\">&#160;<\/p>\n<p style=\"margin: 0px\">&#160; <span style=\"color: gray\">\/\/\/<\/span><span style=\"color: green\"> <\/span><span style=\"color: gray\">&lt;summary&gt;<\/span><\/p>\n<p style=\"margin: 0px\">&#160; <span style=\"color: gray\">\/\/\/<\/span><span style=\"color: green\"> Gets the created files.<\/span><\/p>\n<p style=\"margin: 0px\">&#160; <span style=\"color: gray\">\/\/\/<\/span><span style=\"color: green\"> <\/span><span style=\"color: gray\">&lt;\/summary&gt;<\/span><\/p>\n<p style=\"margin: 0px\">&#160; <span style=\"color: gray\">\/\/\/<\/span><span style=\"color: green\"> <\/span><span style=\"color: gray\">&lt;value&gt;<\/span><span style=\"color: green\">The created files.<\/span><span style=\"color: gray\">&lt;\/value&gt;<\/span><\/p>\n<p style=\"margin: 0px\">&#160; <span style=\"color: blue\">public<\/span> <span style=\"color: #2b91af\">IObservable<\/span>&lt;<span style=\"color: #2b91af\">FileSystemEventArgs<\/span>&gt; CreatedFiles <\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; { <span style=\"color: blue\">get<\/span>; <span style=\"color: blue\">private<\/span> <span style=\"color: blue\">set<\/span>; }<\/p>\n<p style=\"margin: 0px\">&#160;<\/p>\n<p style=\"margin: 0px\">&#160; <span style=\"color: gray\">\/\/\/<\/span><span style=\"color: green\"> <\/span><span style=\"color: gray\">&lt;summary&gt;<\/span><\/p>\n<p style=\"margin: 0px\">&#160; <span style=\"color: gray\">\/\/\/<\/span><span style=\"color: green\"> Gets the deleted files.<\/span><\/p>\n<p style=\"margin: 0px\">&#160; <span style=\"color: gray\">\/\/\/<\/span><span style=\"color: green\"> <\/span><span style=\"color: gray\">&lt;\/summary&gt;<\/span><\/p>\n<p style=\"margin: 0px\">&#160; <span style=\"color: gray\">\/\/\/<\/span><span style=\"color: green\"> <\/span><span style=\"color: gray\">&lt;value&gt;<\/span><span style=\"color: green\">The deleted files.<\/span><span style=\"color: gray\">&lt;\/value&gt;<\/span><\/p>\n<p style=\"margin: 0px\">&#160; <span style=\"color: blue\">public<\/span> <span style=\"color: #2b91af\">IObservable<\/span>&lt;<span style=\"color: #2b91af\">FileSystemEventArgs<\/span>&gt; DeletedFiles <\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; { <span style=\"color: blue\">get<\/span>; <span style=\"color: blue\">private<\/span> <span style=\"color: blue\">set<\/span>; }<\/p>\n<p style=\"margin: 0px\">&#160;<\/p>\n<p style=\"margin: 0px\">&#160; <span style=\"color: gray\">\/\/\/<\/span><span style=\"color: green\"> <\/span><span style=\"color: gray\">&lt;summary&gt;<\/span><\/p>\n<p style=\"margin: 0px\">&#160; <span style=\"color: gray\">\/\/\/<\/span><span style=\"color: green\"> Gets the renamed files.<\/span><\/p>\n<p style=\"margin: 0px\">&#160; <span style=\"color: gray\">\/\/\/<\/span><span style=\"color: green\"> <\/span><span style=\"color: gray\">&lt;\/summary&gt;<\/span><\/p>\n<p style=\"margin: 0px\">&#160; <span style=\"color: gray\">\/\/\/<\/span><span style=\"color: green\"> <\/span><span style=\"color: gray\">&lt;value&gt;<\/span><span style=\"color: green\">The renamed files.<\/span><span style=\"color: gray\">&lt;\/value&gt;<\/span><\/p>\n<p style=\"margin: 0px\">&#160; <span style=\"color: blue\">public<\/span> <span style=\"color: #2b91af\">IObservable<\/span>&lt;<span style=\"color: #2b91af\">RenamedEventArgs<\/span>&gt; RenamedFiles <\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; { <span style=\"color: blue\">get<\/span>; <span style=\"color: blue\">private<\/span> <span style=\"color: blue\">set<\/span>; }<\/p>\n<p style=\"margin: 0px\">}<\/p>\n<\/p><\/div>\n<p>Now we can use the observable and can easily create meta-events:<\/p>\n<div style=\"font-family: courier new; background: white; color: black; font-size: 10pt\">\n<p style=\"margin: 0px\"><span style=\"color: #2b91af\">IDisposable<\/span> writer =<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160; <span style=\"color: blue\">new<\/span> <span style=\"color: #2b91af\">FileSystemObservable<\/span>(<span style=\"color: #a31515\">@&quot;d:\\Test\\&quot;<\/span>, <span style=\"color: #a31515\">&quot;*.*&quot;<\/span>, <span style=\"color: blue\">false<\/span>)<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160;&#160;&#160; .CreatedFiles<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160;&#160;&#160; .Where(x =&gt; (<span style=\"color: blue\">new<\/span> <span style=\"color: #2b91af\">FileInfo<\/span>(x.FullPath)).Length &gt; 0)<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style=\"color: green\">\/\/ &#8230; you can do much more with the combinators<\/span><\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160;&#160;&#160; .Select(x =&gt; x.Name)<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160;&#160;&#160;&#160;&#160; .Subscribe(<span style=\"color: #2b91af\">Console<\/span>.WriteLine);<\/p>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The following class is a wrapper for the System.IO.FileSystemWatcher and converts the FileSystem events into observables. You need to download and reference the Reactive Extensions for .NET (Rx) to use this code: public class FileSystemObservable { &#160; private readonly FileSystemWatcher _fileSystemWatcher; &#160; &#160; &#160; public FileSystemObservable(string directory, &#160;&#160;&#160; string filter, bool includeSubdirectories) &#160; { &#160;&#160;&#160; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[579,578],"_links":{"self":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/931"}],"collection":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/comments?post=931"}],"version-history":[{"count":1,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/931\/revisions"}],"predecessor-version":[{"id":932,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/931\/revisions\/932"}],"wp:attachment":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/media?parent=931"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/categories?post=931"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/tags?post=931"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}