-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSystemUtils.cs
138 lines (122 loc) · 4.04 KB
/
SystemUtils.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
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Sys = Cosmos.System;
namespace JetOS
{
class SystemUtils
{
string ROOT = @"0:\";
string RemoveSides(char[] targets, string src)
{
int first = 0;
int last = src.Length - 1;
int i;
for (i = 0; i < src.Length; i++)
{
if (Array.IndexOf(targets, src[i]) < 0)
{
first = i;
break;
}
}
for (i = src.Length - 1; i >= 0; i--)
{
if (Array.IndexOf(targets, src[i]) < 0)
{
last = i;
break;
}
}
return src.Substring(first, last - first + 1);
}
public void InspectDirectory()
{
string key = Directory.GetCurrentDirectory();
Console.WriteLine("Current directory: {0}", key);
}
public void CreateDirectory(string dir)
{
string currentDir = Directory.GetCurrentDirectory();
char[] targets = { ' ', '\\' };
string targetDir = currentDir + RemoveSides(targets, dir) + @"\";
if (Directory.Exists(targetDir)) {
Console.WriteLine("Cannot create directory. The path you provided has been exist!");
} else
{
Directory.CreateDirectory(targetDir);
Console.WriteLine("Successfully created directory: {0}", targetDir);
}
}
public void RemoveDirectory(string dir)
{
string currentDir = Directory.GetCurrentDirectory();
char[] targets = { ' ', '\\' };
string targetDir = currentDir + RemoveSides(targets, dir) + @"\";
if (!Directory.Exists(targetDir))
{
Console.WriteLine("Cannot remove directory. The path you provided doesn't exist!");
}
else
{
try
{
Directory.Delete(targetDir);
Console.WriteLine("Directory has been removed");
}
catch (Exception ex) {
Console.WriteLine(ex.ToString());
}
}
}
public void ViewDirectoryTree()
{
string currentDir = Directory.GetCurrentDirectory();
string[] dirs = Directory.GetDirectories(currentDir);
string[] files = Directory.GetFiles(currentDir);
int i;
Console.WriteLine("Contents in directroy: {0}\n", currentDir);
for (i = 0; i < dirs.Length; i++)
{
Console.WriteLine("-> {0}", dirs[i]);
}
for (i = 0; i < files.Length; i++)
{
Console.WriteLine("{0}", files[i]);
}
Console.WriteLine("\n");
}
public void RenameDirectory()
{
Console.WriteLine("Unavailable");
}
public void GoToDirectory(string dirChunk)
{
try
{
string currentDir = Directory.GetCurrentDirectory();
char[] targets = { ' ', '\\' };
string targetDir = currentDir + RemoveSides(targets, dirChunk) + @"\";
if (Directory.Exists(targetDir))
{
Directory.SetCurrentDirectory(targetDir);
}
else if (RemoveSides(targets, dirChunk) == "~") {
Directory.SetCurrentDirectory(ROOT);
}
else {
Console.WriteLine("Cannot find directory: {0}", targetDir);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
public void Say(string msg)
{
Console.WriteLine(msg);
}
}
}