-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
519 lines (494 loc) · 22.7 KB
/
Program.cs
File metadata and controls
519 lines (494 loc) · 22.7 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
using ConsoleShortPathName;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleFileManager
{
class Program
{
const int WINDOW_HEGHT = 50;
const int WINDOW_WIDTH = 120;
private static string currentDir = Properties.Settings.Default.LastPath;
static void Main()
{
Console.BackgroundColor = ConsoleColor.DarkCyan;
Console.ForegroundColor = ConsoleColor.White;
Console.Title = "ConsoleFileManager";
Console.SetWindowSize(WINDOW_WIDTH, WINDOW_HEGHT);
Console.SetBufferSize(WINDOW_WIDTH, WINDOW_HEGHT);
DrawWindow(0, 0, WINDOW_WIDTH, 38);
DrawWindow(0, 38, WINDOW_WIDTH, 8);
bool b = true;
try
{
while (b)
{
Console.SetCursorPosition(2, 39);
Console.WriteLine("Для работы с консольным менеджером нажмите 1.");
Console.SetCursorPosition(2, 40);
Console.WriteLine("Для выхода из консольного менеджера в меню введите exit.");
Console.SetCursorPosition(2, 41);
Console.Write("Чтобы завершить работу программу введите 0: ");
int TaskNumber = Convert.ToInt32(Console.ReadLine());
DrawWindow(0, 38, WINDOW_WIDTH, 8);
switch (TaskNumber)
{
case 1:
UpdateConsole();
break;
case 0:
b = false;
Console.SetCursorPosition(2, 39);
Console.WriteLine("Завершение работы приложения...");
Console.ReadKey(true);
Process.GetCurrentProcess().Kill();
break;
default:
DrawWindow(0, 38, WINDOW_WIDTH, 8);
Console.SetCursorPosition(2, 39);
Console.WriteLine("Повторите ввод.");
Console.ReadKey(true);
break;
}
}
}
catch (Exception errors)
{
DateTime date = DateTime.Now;
DrawWindow(0, 38, WINDOW_WIDTH, 8);
Console.SetCursorPosition(1, 39);
Console.WriteLine("Обнаружена ошибка: " + errors.Message);
Console.ReadKey(true);
if (!File.Exists($@"{Environment.CurrentDirectory}\random-name-exception.txt"))
{
File.Create($@"{Environment.CurrentDirectory}\random-name-exception.txt");
}
File.AppendAllText($@"{Environment.CurrentDirectory}\random-name-exception.txt", ($"{date} {errors.Message} \n"));
}
finally
{
UpdateConsole();
DrawWindow(0, 38, WINDOW_WIDTH, 8);
Main();
}
}
/// <summary>
/// Метод для отрисовки окна
/// </summary>
/// <param name="x"> точка по оси Х</param>
/// <param name="y">точка по оси Y</param>
/// <param name="width">ширина окна</param>
/// <param name="height"> высота окна</param>
static void DrawWindow(int x, int y, int width, int height)
{
//шапка
Console.SetCursorPosition(x, y);
Console.Write("┌");
for (int i = 0; i < width - 2; i++)
Console.Write("─");
Console.Write("┐");
//окно
Console.SetCursorPosition(x, y + 1);
for (int i = 0; i < height - 2; i++)
{
Console.Write("│");
for (int j = x + 1; j < x + width - 1; j++)
Console.Write(" ");
Console.Write("│");
}
//подвал
Console.Write("└");
for (int i = 0; i < width - 2; i++)
Console.Write("─");
Console.Write("┘");
Console.SetCursorPosition(x, y);
}
/// <summary>
/// Метод для отрисовки консоли
/// </summary>
/// <param name="dir">текущая директория</param>
/// <param name="x"> точка по оси Х</param>
/// <param name="y">точка по оси Y</param>
/// <param name="width">ширина окна</param>
/// <param name="height"> высота окна</param>
static void DrawConsole(string dir, int x, int y, int width, int height)
{
DrawWindow(x, y, width, height);
Console.SetCursorPosition(x + 1, y + height / 2);
Console.Write($"{dir}>");
}
/// <summary>
/// Метод для обновления ввода консоли
/// </summary>
static void UpdateConsole()
{
DrawConsole(GetShortPathName(currentDir), 0, 46, WINDOW_WIDTH, 3);
ProcessEnterCommand(WINDOW_WIDTH);
}
/// <summary>
/// Вспомогательный метод для получения позиции курсора
/// </summary>
/// <returns></returns>
static (int left, int top) GetCursorPosition()
{
return (Console.CursorLeft, Console.CursorTop);
}
/// <summary>
/// Метод для обработки процесса ввода консоли
/// <param name="width">длина строки ввода</param>
/// </summary>
static void ProcessEnterCommand(int width)
{
(int left, int top) = GetCursorPosition();
StringBuilder command = new StringBuilder();
ConsoleKeyInfo keyInfo;
char key;
do
{
keyInfo = Console.ReadKey();
key = keyInfo.KeyChar;
if (keyInfo.Key != ConsoleKey.Enter && keyInfo.Key != ConsoleKey.Backspace &&
keyInfo.Key != ConsoleKey.UpArrow)
command.Append(key);
(int currentLeft, int currentTop) = GetCursorPosition();
if (currentLeft == width - 2)
{
Console.SetCursorPosition(currentLeft - 1, top);
Console.Write(" ");
Console.SetCursorPosition(currentLeft - 1, top);
}
if (keyInfo.Key == ConsoleKey.Backspace)
{
if (command.Length > 0)
command.Remove(command.Length - 1, 1);
if (currentLeft >= left)
{
Console.SetCursorPosition(currentLeft, top);
Console.Write(" ");
Console.SetCursorPosition(currentLeft, top);
}
else
{
command.Clear();
Console.SetCursorPosition(left, top);
}
}
}
while (keyInfo.Key != ConsoleKey.Enter);
ParseCommandString(command.ToString());
}
/// <summary>
/// Метод для обработки команд
/// </summary>
/// <param name="command">Вводимая команда</param>
private static void ParseCommandString(string command)
{
string[] commandParams = command.ToLower().Split(' ');
if (commandParams.Length > 0)
{
bool b = true;
while (b)
{
switch (commandParams[0])
{
case "cd":
if (commandParams.Length > 1 && Directory.Exists(commandParams[1]))
{
currentDir = commandParams[1];
}
else if (commandParams.Length > 1 && !Directory.Exists(commandParams[1]))
{
commandParams = command.ToLower().Split('\"');
currentDir = commandParams[1].Trim();
}
UpdateConsole();
break;
case "ls":
if (commandParams.Length > 1 && Directory.Exists(commandParams[1]))
{
if (commandParams.Length > 3 && commandParams[2] == "-p" && int.TryParse(commandParams[3], out int n))
{
DrawTree(new DirectoryInfo(commandParams[1]), n);
}
else
{
DrawTree(new DirectoryInfo(commandParams[1]), 1);
}
}
else if (commandParams.Length > 1 && !Directory.Exists(commandParams[1]))
{
commandParams = command.ToLower().Split('\"');
if (commandParams.Length > 3 && commandParams[2].Trim() == "-p" && int.TryParse(commandParams[3].Trim(), out int n))
{
DrawTree(new DirectoryInfo(commandParams[1]), n);
}
else
{
DrawTree(new DirectoryInfo(commandParams[1]), 1);
}
}
UpdateConsole();
break;
case "cp":
if (commandParams.Length > 2)
{
if (File.Exists(commandParams[1]))
{
if (Directory.Exists(commandParams[2]))
{
File.Create(commandParams[2]);
File.Copy(commandParams[1], commandParams[2], true);
}
else if (!Directory.Exists(commandParams[2]))
{
Directory.CreateDirectory(commandParams[2]);
File.Create(commandParams[2]);
File.Copy(commandParams[1], commandParams[2], true);
}
}
else if (Directory.Exists(commandParams[1]))
{
if (!Directory.Exists(commandParams[2]))
{
Directory.CreateDirectory(commandParams[2]);
}
else
{
CopyDir(commandParams[1], commandParams[2], true);
}
}
}
else if (commandParams.Length > 2)
{
commandParams = command.ToLower().Split('\"');
if (File.Exists(commandParams[1].Trim()))
{
if (Directory.Exists(commandParams[1].Trim()))
{
File.Create(commandParams[3].Trim());
File.Copy(commandParams[1].Trim(), commandParams[3].Trim(), true);
}
else if (!Directory.Exists(commandParams[3].Trim()))
{
Directory.CreateDirectory(commandParams[3].Trim());
File.Create(commandParams[3].Trim());
File.Copy(commandParams[1].Trim(), commandParams[3].Trim(), true);
}
}
else if (Directory.Exists(commandParams[1].Trim()))
{
if (!Directory.Exists(commandParams[3].Trim()))
{
Directory.CreateDirectory(commandParams[3].Trim());
CopyDir(commandParams[1].Trim(), commandParams[3].Trim(), true);
}
else
{
CopyDir(commandParams[1].Trim(), commandParams[3].Trim(), true);
}
}
}
UpdateConsole();
break;
case "file":
if (commandParams.Length > 1)
{
if (File.Exists(commandParams[1]))
{
DrawWindow(0, 38, WINDOW_WIDTH, 8);
Info(new FileInfo(commandParams[1]));
}
else if (Directory.Exists(commandParams[1]))
{
DrawWindow(0, 38, WINDOW_WIDTH, 8);
DIRInfo(new DirectoryInfo(commandParams[1]));
}
}
else
{
commandParams = command.ToLower().Split('\"');
if (File.Exists(commandParams[1].Trim()))
{
DrawWindow(0, 38, WINDOW_WIDTH, 8);
Info(new FileInfo(commandParams[1].Trim()));
}
else if (Directory.Exists(commandParams[1].Trim()))
{
DrawWindow(0, 38, WINDOW_WIDTH, 8);
DIRInfo(new DirectoryInfo(commandParams[1].Trim()));
}
}
UpdateConsole();
break;
case "rm":
if (commandParams.Length > 1)
{
if (File.Exists(commandParams[1]))
{
File.Delete(commandParams[1]);
}
else if (Directory.Exists(commandParams[1]))
{
Directory.Delete(commandParams[1], true);
}
}
UpdateConsole();
break;
case "exit":
b = false;
GC.Collect();
DrawWindow(0, 46, WINDOW_WIDTH, 3);
Main();
UpdateConsole();
break;
default:
GC.Collect();
UpdateConsole();
break;
}
}
}
}
/// <summary>
/// Метод для копирования директории
/// </summary>
/// <param name="fromDir">Начальная директория</param>
/// <param name="toDir">Конечная директория</param>
/// <param name="recursive"></param>
static void CopyDir (string fromDir, string toDir, bool recursive)
{
var dir = new DirectoryInfo(fromDir);
if (!dir.Exists)
{
return;
}
DirectoryInfo[] dirs = dir.GetDirectories();
Directory.CreateDirectory(toDir);
foreach (FileInfo file in dir.GetFiles())
{
string targetFilePath = Path.Combine(toDir, file.Name);
file.CopyTo(targetFilePath);
}
if (recursive)
{
foreach (DirectoryInfo subDir in dirs)
{
string newDestinationDir = Path.Combine(toDir, subDir.Name);
CopyDir(subDir.FullName, newDestinationDir, true);
}
}
}
/// <summary>
/// Метод для укорачивания пути директории
/// </summary>
/// <param name="path"> текущая директория</param>
/// <returns></returns>
static string GetShortPathName(string path)
{
StringBuilder shortPathName = new StringBuilder((int)API.MAX_PATH);
API.GetShortPathName(path, shortPathName, API.MAX_PATH);
return shortPathName.ToString();
}
/// <summary>
/// Метод для получения дерева файлов
/// </summary>
/// <param name="tree">запись в динамическую строку</param>
/// <param name="dir">текущая директория</param>
/// <param name="indent">пробел</param>
/// <param name="lastDirectory">проверка на крайность директории</param>
static void GetTree(StringBuilder tree, DirectoryInfo dir, string indent, bool lastDirectory)
{
tree.Append(indent);
if (lastDirectory)
{
tree.Append("└─");
indent += " ";
}
else
{
tree.Append("├─");
}
tree.Append($"{dir.Name}\n");
FileInfo[] subFiles = dir.GetFiles();
for (int i = 0; i < subFiles.Length; i++)
{
if (i == subFiles.Length)
{
tree.Append($"{indent}└─{subFiles[i].Name}\n ");
}
else
{
tree.Append($"{indent}├─{subFiles[i].Name}\n");
}
DirectoryInfo[] subDirects = dir.GetDirectories();
for (int j = 0; j < subDirects.Length; j++)
GetTree(tree, subDirects[j], indent, j == subDirects.Length - 1);
}
}
/// <summary>
/// Метод для отрисовки дерева
/// </summary>
/// <param name="dir"> текущая директория</param>
/// <param name="page">количсество отрображаемых строк на странице</param>
static void DrawTree(DirectoryInfo dir, int page)
{
StringBuilder tree = new StringBuilder();
GetTree(tree, dir, "", true);
DrawWindow(0, 0, WINDOW_WIDTH, 38);
(int currentLeft, int currentTop) = GetCursorPosition();
int pageLines = 36;
string[] lines = tree.ToString().Split('\n');
int pageTotal = (lines.Length + pageLines - 1) / pageLines;
if (page > pageTotal)
page = pageTotal;
for (int i = (page-1)*pageLines, counter = 0; i<page*pageLines; i++, counter++)
{
if (lines.Length-1>i)
{
Console.SetCursorPosition(currentLeft + 1, currentTop + 1 + counter);
Console.WriteLine(lines[i]);
}
}
//Отрисовка количества страниц
string footer = $"┤ {page} of {pageTotal} ├";
Console.SetCursorPosition(WINDOW_WIDTH / 2 - footer.Length / 2, 37);
Console.WriteLine(footer);
}
///Метод для вывода информации файле
static void Info(FileInfo file)
{
Console.SetCursorPosition(2, 39);
Console.Write("Аттрибуты файла\t");
Console.WriteLine(file.Attributes);
Console.SetCursorPosition(2, 40);
Console.Write("Время создания файла\t");
Console.WriteLine(file.CreationTime);
Console.SetCursorPosition(2, 41);
Console.Write("Время последнего доступа к файлу\t");
Console.WriteLine(file.LastAccessTime);
Console.SetCursorPosition(2, 43);
Console.Write("Размер файла в байтах\t");
Console.WriteLine(file.Length);
}
// Метод для вывода информации о папке
static void DIRInfo(DirectoryInfo dir)
{
Console.SetCursorPosition(2, 39);
Console.Write("Аттрибуты директории\t");
Console.WriteLine(dir.Attributes);
Console.SetCursorPosition(2, 40);
Console.Write("Время создания директории\t");
Console.WriteLine(dir.CreationTime);
Console.SetCursorPosition(2, 41);
Console.Write("Время последнего доступа к каталогу\t");
Console.WriteLine(dir.LastAccessTime);
Console.SetCursorPosition(2, 43);
Console.Write("Путь к директории\t");
Console.WriteLine(dir.FullName);
}
}
}