This repository has been archived by the owner on Feb 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
traversal.cs
145 lines (118 loc) · 5.18 KB
/
traversal.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
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System;
using System.IO;
using System.Linq;
using System.Security.Permissions;
namespace FSTraversal
{
class FSTraversal
{
///////////////////////main/////////////////////////
static void Main(string[] args)
{
Console.WriteLine("Traversal Started.\n");
if(args == null || args.Length != 1)
{
Console.WriteLine("ERROR: INVALID ARGUMENT");
return;
}
string path = args[0];
bool confirmed = false;
string Key;
Console.WriteLine("You have defined the following dir: {0}\n", path);
//ShowAllFoldersUnder(path, 0);
// Keep the console window open in debug mode.
///TODO: Need to clean this up
do {
Key = Console.ReadLine();
ConsoleKey response;
do
{
Console.Write("Are you sure you want to enable Watchery on this dir? [y/n] ");
response = Console.ReadKey(false).Key; // true is intercept key (dont show), false is show
if (response != ConsoleKey.Enter)
Console.WriteLine();
} while (response != ConsoleKey.Y && response != ConsoleKey.N);
confirmed = response == ConsoleKey.Y;
} while (!confirmed);
Console.WriteLine(" ACTIVATING ON {0}...", path);
Console.ReadLine();
Console.ReadKey();
} //main
private static void ShowAllFoldersUnder(string path, int indent)
{
if (path == null) return;
try
{
//////TRAVERSE OPTION 1 : LINQ
if (Directory.Exists(path))
Directory.GetFiles(path).ToList().ForEach(s => { gFiInfoviaPath(s); });
else
Console.WriteLine("ERROR: PATH NOT FOUND");
//////TRAVERSE OPTION 2 : RECURSIVE
// if ((File.GetAttributes(path) & FileAttributes.ReparsePoint)!= FileAttributes.ReparsePoint)
// {
// foreach (string folder in Directory.GetDirectories(path))
// {
// Console.WriteLine("{0}{1}", new string('-', indent), Path.GetFileName(folder));
// ShowAllFoldersUnder(folder, indent + 2);
// }
// }
}
catch (UnauthorizedAccessException) { }
}
private static void gFiInfoviaPath(string path)
{
try
{
string extension = Path.GetExtension(path);
string filename = Path.GetFileName(path);
string filenameNoExtension = Path.GetFileNameWithoutExtension(path);
string root = Path.GetPathRoot(path);
Console.WriteLine("\nFile Extenson: {0}\n --> FILENAME: {1}\nRoot Dir: {2}\n",extension,filenameNoExtension,root);
}
catch (UnauthorizedAccessException) { }
}
[PermissionSet(SecurityAction.Demand, Name="FullTrust")]
public static void Run()
{
string[] args = System.Environment.GetCommandLineArgs();
// If a directory is not specified, exit program.
if(args.Length != 2)
{
// Display the proper way to call the program.
Console.WriteLine("Usage: Watcher.exe (directory)");
return;
}
// Create a new FileSystemWatcher and set its properties.
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = args[1];
/* 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 += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
// Begin watching.
watcher.EnableRaisingEvents = true;
// Wait for the user to quit the program.
Console.WriteLine("Press \'q\' to quit the sample.");
while(Console.Read()!='q');
}
// Define the event handlers.
private static void OnChanged(object source, FileSystemEventArgs e)
{
// Specify what is done when a file is changed, created, or deleted.
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
}
private static void OnRenamed(object source, RenamedEventArgs e)
{
// Specify what is done when a file is renamed.
Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
}
} //class
}