forked from idg10/prog-cs-8-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrouping.cs
33 lines (30 loc) · 1.14 KB
/
Grouping.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using System;
using System.IO;
using System.Reactive;
using System.Reactive.Linq;
namespace LinqQueries
{
public static class Grouping
{
public static void GroupingEvents()
{
string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var w = new FileSystemWatcher(path);
IObservable<EventPattern<FileSystemEventArgs>> changes =
Observable.FromEventPattern<FileSystemEventHandler, FileSystemEventArgs>(
h => w.Changed += h, h => w.Changed -= h);
w.IncludeSubdirectories = true;
w.EnableRaisingEvents = true;
IObservable<IGroupedObservable<string, string>> folders =
from change in changes
group Path.GetFileName(change.EventArgs.FullPath)
by Path.GetDirectoryName(change.EventArgs.FullPath);
folders.Subscribe(f =>
{
Console.WriteLine("New folder ({0})", f.Key);
f.Subscribe(file =>
Console.WriteLine("File changed in folder {0}, {1}", f.Key, file));
});
}
}
}