-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
129 lines (127 loc) · 4.26 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
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
using System;
using System.Diagnostics;
using System.IO;
namespace MSH
{
class Program
{
static void Main(string[] args)
{
Directory.SetCurrentDirectory(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
int status;
do
{
Console.ResetColor();
DateTime date = DateTime.Now;
Console.Write("[{0:g}] ", date);
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(Environment.UserName + "@" + Environment.MachineName);
Console.ResetColor();
Console.Write(":");
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write(Directory.GetCurrentDirectory());
Console.ResetColor();
Console.Write("> ");
Console.ResetColor();
string[] input = Console.ReadLine().Split(' ',2);
status = Exec(input);
if (status != 0 && status != -1 && status != -2)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("X ");
}
else if (status == 0)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("V ");
}
else if (status == -2)
{
Console.ResetColor();
}
else
{
Console.ResetColor();
}
} while (status != -1);
}
static int Exec(string[] input)
{
if (input[0] == "connect")
{
Console.WriteLine("This feature is still being worked on.");
return 0;
} else if (input[0] == "exit")
{
return -1;
} else if (input[0] == "clear")
{
Console.Clear();
return -2;
} else if (input[0] == "cd")
{
if (!(input.Length >= 2))
{
Console.WriteLine("Please specify a path");
Console.Beep();
return 1;
}
try
{
Directory.SetCurrentDirectory(input[1]);
return 0;
}
catch (DirectoryNotFoundException)
{
Console.WriteLine("msh: cd: Directory not found");
return 1;
}
} else if (input[0] == "ls")
{
string[] files = Directory.GetFileSystemEntries(Directory.GetCurrentDirectory());
foreach (string file in files)
{
Console.WriteLine(file);
}
return 0;
} else
{
Process process;
if (input.Length >= 2)
{
try
{
process = Process.Start(input[0],input[1]);
process.WaitForExit();
return process.ExitCode;
}
catch (System.ComponentModel.Win32Exception)
{
Console.WriteLine("msh: " + input[0] + ": command not found.");
return 1;
} catch (InvalidOperationException)
{
return -2;
}
}
else
{
try
{
process = Process.Start(input[0]);
process.WaitForExit();
return process.ExitCode;
}
catch (System.ComponentModel.Win32Exception)
{
Console.WriteLine("msh: " + input[0] + ": command not found.");
return 1;
} catch (InvalidOperationException)
{
return -2;
}
}
}
}
}
}