-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathFileWatcher.cs
More file actions
61 lines (54 loc) · 1.56 KB
/
FileWatcher.cs
File metadata and controls
61 lines (54 loc) · 1.56 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
using System;
using System.IO;
using System.Collections.Concurrent;
using UnityEngine;
public class FileWatcher : MonoBehaviour
{
private FileSystemWatcher fileSystemWatcher;
private readonly ConcurrentQueue<string> concurrentQueue = new ConcurrentQueue<string>();
void Start()
{
string fullPath = Path.GetFullPath(Application.persistentDataPath);
Debug.Log(fullPath);
if (!Directory.Exists(fullPath))
{
Debug.LogError($"Directory not exists: {fullPath}");
return;
}
fileSystemWatcher = new FileSystemWatcher(fullPath, "*.txt");
fileSystemWatcher.IncludeSubdirectories = true;
fileSystemWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName;
fileSystemWatcher.Changed += OnChanged;
fileSystemWatcher.Created += OnChanged;
fileSystemWatcher.Renamed += OnRenamed;
fileSystemWatcher.Deleted += OnDeleted;
fileSystemWatcher.EnableRaisingEvents = true;
}
void OnChanged(object sender, FileSystemEventArgs e)
{
concurrentQueue.Enqueue($"Changed file: {e.FullPath}");
}
void OnDeleted(object sender, FileSystemEventArgs e)
{
concurrentQueue.Enqueue($"Deleted file: {e.FullPath}");
}
void OnRenamed(object sender, RenamedEventArgs e)
{
concurrentQueue.Enqueue($"Renamed file {e.OldFullPath} to {e.FullPath}");
}
void Update()
{
while (concurrentQueue.TryDequeue(out string change))
{
Debug.Log(change);
}
}
void OnDestroy()
{
if (fileSystemWatcher != null)
{
fileSystemWatcher.EnableRaisingEvents = false;
fileSystemWatcher.Dispose();
}
}
}