-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileManager.cs
43 lines (35 loc) · 1.17 KB
/
FileManager.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
34
35
36
37
38
39
40
41
42
43
using System;
using System.IO;
namespace SaveFileWatcher
{
public class FileWatcher
{
private string filePath;
private FileSystemWatcher fileSystemWatcher;
public FileWatcher(string filePath)
{
this.filePath = filePath;
fileSystemWatcher = new FileSystemWatcher(Path.GetDirectoryName(filePath)!, Path.GetFileName(filePath));
fileSystemWatcher.Changed += OnFileChanged;
fileSystemWatcher.Created += OnFileChanged;
}
public void StartWatching()
{
fileSystemWatcher.EnableRaisingEvents = true;
Console.WriteLine($"Watching file: {filePath}");
Console.WriteLine("Press any key to stop watching...");
Console.ReadKey();
StopWatching();
}
private void OnFileChanged(object sender, FileSystemEventArgs e)
{
Console.WriteLine($"File changed: {e.FullPath}");
}
private void StopWatching()
{
fileSystemWatcher.EnableRaisingEvents = false;
fileSystemWatcher.Dispose();
Console.WriteLine("Stopped watching file.");
}
}
}