-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileManager.cs
More file actions
329 lines (300 loc) · 11.5 KB
/
FileManager.cs
File metadata and controls
329 lines (300 loc) · 11.5 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FileManagerCSharp
{
internal class FileManager
{
private string _currentDir = Directory.GetCurrentDirectory();
private List<string> _history = new List<string>();
public string CurrentDir
{
get
{
return _currentDir;
}
}
public void ShowDir()
{
Console.WriteLine();
Console.WriteLine("Directory of " + _currentDir);
Console.WriteLine();
foreach (string d in Directory.GetDirectories(_currentDir))
{
Console.WriteLine("<DIR> " + Path.GetFileName(d));
}
foreach (string f in Directory.GetFiles(_currentDir))
{
FileInfo file = new FileInfo(f);
Console.WriteLine("<FILE> " + file.Name + " " + file.Length + " bytes");
}
Console.WriteLine();
}
public void ChangeDir(string path)
{
if (path == "..")
{
DirectoryInfo parent = Directory.GetParent(_currentDir);
if (parent != null)
_currentDir = parent.FullName;
else
Console.WriteLine("Already at root directory, you cant go higher.");
return;
}
string fullPath = Path.Combine(_currentDir, path);
if (Directory.Exists(fullPath))
_currentDir = fullPath;
else
Console.WriteLine("Directory not found --> " + path);
}
public void CreateDir(string name)
{
string path = Path.Combine(_currentDir, name);
Directory.CreateDirectory(path);
Console.WriteLine("Directory created --> " + name);
}
public void DeleteDir(string name)
{
string path = Path.Combine(_currentDir, name);
if (!Directory.Exists(path))
{
Console.WriteLine("Directory not found --> " + name);
return;
}
Directory.Delete(path, true);
Console.WriteLine("Directory deleted --> " + name);
}
public void CreateFile(string name, string text)
{
string path = Path.Combine(_currentDir, name);
using (StreamWriter sw = new StreamWriter(path, false, Encoding.UTF8))
{
sw.WriteLine(text);
}
Console.WriteLine("File created --> " + name);
}
public void ReadFile(string name)
{
string path = Path.Combine(_currentDir, name);
if (!File.Exists(path))
{
Console.WriteLine("File not found --> " + name);
return;
}
Console.WriteLine();
using (StreamReader sr = new StreamReader(path, Encoding.UTF8))
{
Console.WriteLine(sr.ReadToEnd());
}
}
public void DeleteFile(string name)
{
string path = Path.Combine(_currentDir, name);
if (!File.Exists(path))
{
Console.WriteLine("File not found --> " + name);
return;
}
File.Delete(path);
Console.WriteLine("File deleted --> " + name);
}
public void Copy(string source, string dest)
{
string fullSource = Path.Combine(_currentDir, source);
string fullDest = Path.Combine(_currentDir, dest);
if (File.Exists(fullSource))
{
File.Copy(fullSource, fullDest, true);
Console.WriteLine("File copied --> " + source + " to " + dest);
}
else if (Directory.Exists(fullSource))
{
CopyDir(fullSource, fullDest);
Console.WriteLine("Directory copied --> " + source + " to " + dest);
}
else
{
Console.WriteLine("Not found --> " + source);
}
}
private void CopyDir(string source, string dest)
{
Directory.CreateDirectory(dest);
foreach (FileInfo file in new DirectoryInfo(source).GetFiles())
{
file.CopyTo(Path.Combine(dest, file.Name), true);
}
foreach (DirectoryInfo subDir in new DirectoryInfo(source).GetDirectories())
{
CopyDir(subDir.FullName, Path.Combine(dest, subDir.Name));
}
}
public void Move(string source, string dest)
{
string fullSource = Path.Combine(_currentDir, source);
string fullDest = Path.Combine(_currentDir, dest);
if (File.Exists(fullSource))
{
File.Move(fullSource, fullDest);
Console.WriteLine("File moved --> " + source + " to " + dest);
}
else if (Directory.Exists(fullSource))
{
CopyDir(fullSource, fullDest);
Directory.Delete(fullSource, true);
Console.WriteLine("Directory moved --> " + source + " to " + dest);
}
else
{
Console.WriteLine("Not found --> " + source);
}
}
public void ShowAttr(string name)
{
string path = Path.Combine(_currentDir, name);
if (!File.Exists(path))
{
Console.WriteLine("File not found --> " + name);
return;
}
FileInfo info = new FileInfo(path);
Console.WriteLine();
Console.WriteLine("Name --> " + info.Name);
Console.WriteLine("Size --> " + info.Length + " bytes");
Console.WriteLine("Created --> " + info.CreationTime);
Console.WriteLine("Modified --> " + info.LastWriteTime);
Console.WriteLine("Attributes --> " + info.Attributes);
Console.WriteLine();
}
public void Search(string name)
{
Console.WriteLine();
Console.WriteLine("Searching --> " + name);
Console.WriteLine();
int found = 0;
SearchInDir(new DirectoryInfo(_currentDir), name, ref found);
Console.WriteLine("Found " + found + " result(s)");
Console.WriteLine();
}
private void SearchInDir(DirectoryInfo dir, string name, ref int found)
{
try
{
foreach (FileInfo file in dir.GetFiles())
{
if (file.Name.Contains(name))
{
Console.WriteLine(file.FullName);
found++;
}
}
foreach (DirectoryInfo subDir in dir.GetDirectories())
{
if (subDir.Name.Contains(name))
{
Console.WriteLine(subDir.FullName);
found++;
}
SearchInDir(subDir, name, ref found);
}
}
catch
{
Console.WriteLine("Access denied --> " + dir.FullName);
}
}
public void AddHistory(string command)
{
_history.Add(command);
}
public void ShowHistory()
{
if (_history.Count == 0)
{
Console.WriteLine("History is empty.");
return;
}
Console.WriteLine();
for (int i = 0; i < _history.Count; i++)
{
Console.WriteLine((i + 1) + ". " + _history[i]);
}
Console.WriteLine();
}
public void ShowHelp()
{
Console.WriteLine();
Console.WriteLine("Available commands:");
Console.WriteLine("-------------------");
Console.WriteLine("dir - show files and folders");
Console.WriteLine("cd - change directory");
Console.WriteLine("mkdir - create directory");
Console.WriteLine("rmdir - delete directory");
Console.WriteLine("create - create text file");
Console.WriteLine("read - read text file");
Console.WriteLine("del - delete file");
Console.WriteLine("copy - copy file or folder");
Console.WriteLine("move - move file or folder");
Console.WriteLine("attr - show file attributes");
Console.WriteLine("search - search files");
Console.WriteLine("history - show command history");
Console.WriteLine("cls - clear screen");
Console.WriteLine("help - show this help");
Console.WriteLine("exit - exit program");
Console.WriteLine();
}
public void ShowHelpDetail(string command)
{
Console.WriteLine();
switch (command.ToLower())
{
case "dir":
Console.WriteLine("dir - shows all files and folders in current directory");
break;
case "cd":
Console.WriteLine("cd - change directory");
Console.WriteLine("type .. to go back to base folder");
break;
case "mkdir":
Console.WriteLine("mkdir - creates new folder in current directory");
break;
case "rmdir":
Console.WriteLine("rmdir - deletes folder with all files");
break;
case "create":
Console.WriteLine("create - creates a new text file with text");
break;
case "read":
Console.WriteLine("read - shows text of a text file");
break;
case "del":
Console.WriteLine("del - deletes a file");
break;
case "copy":
Console.WriteLine("copy - copies file or folder to new place");
break;
case "move":
Console.WriteLine("move - moves file or folder to new place");
break;
case "attr":
Console.WriteLine("attr - shows name, size, dates and attributes of file");
break;
case "search":
Console.WriteLine("search - searches files by name in current folder and subfolders");
break;
case "history":
Console.WriteLine("history - shows list of all entered commands");
break;
case "cls":
Console.WriteLine("cls - clears the screen");
break;
default:
Console.WriteLine("No help for --> " + command);
break;
}
Console.WriteLine();
}
}
}