-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
45 lines (40 loc) · 1.41 KB
/
Program.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
44
45
public class Program
{
public static void Main()
{
string userName = Environment.UserName;
WatchDirectory($"C:\\Users\\{userName}");
}
private static void WatchDirectory(string directoryPath)
{
using (var watcher = new FileSystemWatcher(directoryPath))
{
watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.Filter = "*.*";
watcher.Created += OnFileCreated;
watcher.Renamed += OnFileRenamed;
watcher.EnableRaisingEvents = true;
Console.WriteLine("Watching for file/folder changes...");
Console.WriteLine("Press 'q' to quit.");
while (Console.Read() != 'q') ;
}
}
private static void OnFileCreated(object sender, FileSystemEventArgs e)
{
Console.WriteLine($"File/Folder Added: {e.FullPath}");
Check(e.FullPath);
}
private static void OnFileRenamed(object sender, RenamedEventArgs e)
{
Console.WriteLine($"File/Folder Renamed: {e.FullPath}");
Check(e.FullPath);
}
private static void Check(string filename)
{
string name = Path.GetFileName(filename);
if (name.StartsWith('.') && !File.GetAttributes(filename).HasFlag(FileAttributes.Hidden))
{
File.SetAttributes(filename, File.GetAttributes(filename) | FileAttributes.Hidden);
}
}
}