Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
77 lines (62 loc) · 2.63 KB
/
Copy pathProgram.cs
File metadata and controls
77 lines (62 loc) · 2.63 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;
using System.IO;
namespace FSWTest
{
class Program
{
static void Main(string[] args)
{
string tmpDir = Environment.GetEnvironmentVariable("TMPDIR")
?? Environment.GetEnvironmentVariable("TEMP")
?? Environment.GetEnvironmentVariable("TMP");
if (string.IsNullOrEmpty(tmpDir)) throw new Exception("Please set environment variable TMPDIR or TEMP or TMP");
string watchDir = Path.Combine(tmpDir, "FSWTest");
string filename = DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt";
string filePath = Path.Combine(watchDir, filename);
PrepareDirectory(watchDir);
CreateFiles(filePath);
File.Delete(filePath);
using(var watcher = CreateFileSystemWatcher(watchDir))
{
CreateFiles(filePath);
Console.WriteLine("Press 'q' to quit the sample.");
while (Console.Read() != 'q') { }
}
}
private static void CreateFiles(string filePath)
{
File.WriteAllText(filePath, "something");
}
private static void PrepareDirectory(string watchDir)
{
if (Directory.Exists(watchDir))
Directory.Delete(watchDir, true);
Directory.CreateDirectory(watchDir);
}
private static FileSystemWatcher CreateFileSystemWatcher(string watchDir)
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = watchDir;
// Watch for changes in LastAccess and LastWrite times, and
// the renaming of files or directories.
watcher.NotifyFilter = NotifyFilters.LastAccess
| NotifyFilters.LastWrite
| NotifyFilters.FileName
| NotifyFilters.DirectoryName;
// Only watch text files.
watcher.Filter = "*.txt";
// Add event handlers.
watcher.Changed += OnChanged;
watcher.Created += OnChanged;
watcher.Deleted += OnChanged;
watcher.Renamed += OnRenamed;
// Begin watching.
watcher.EnableRaisingEvents = true;
return watcher;
}
private static void OnChanged(object source, FileSystemEventArgs e) =>
Console.WriteLine($"File: {e.FullPath} {e.ChangeType}");
private static void OnRenamed(object source, RenamedEventArgs e) =>
Console.WriteLine($"File: {e.OldFullPath} renamed to {e.FullPath}");
}
}