The school I go to places ridiculous restrictions on the computers. Right clicks aside, one thing I wish above all things they hadn't disabled was the command prompt. Ironically, the computers also have support for a number of programming languages. A friend of mine managed to emulate one using Python, and all was swell. The best part was that he coded in functionality to automatically add things to the system path. You see, we were doing quite a bit of C++/MinGW at the time... off our USBs of course.
I decided to make my own. The Python script had the added bonus of being cross platform, but it would be quite the stretch to expect every Windows machine in the world to have Python installed. I wanted my own custom command prompt. Because I rarely use Mac/Linux, .NET is installed on all up-to-date Windows machines, and it's simply an awesome language, I decided to use C#.
Basic Command Prompt
Creating a basic command prompt is easy. Being a simple program, I broke my practices and shoved most of the code in the Main method, but here it is:
[source lang=csharp]
public static void Main(string[] args)
{
while (true)
{
Console.Write(Directory.GetCurrentDirectory() + ">");
var input = Console.ReadLine().Trim();
if (input.Length >= 2 && input.Substring(0, 2).ToLower() == "cd")
ChangeDirectory(input);
else if (input.ToLower() == "exit")
break;
else
system(input);
}
}
[/source]
There are two special cases checked for before doing normal processing of the input command. The first is a cd command. This has to be handled manually, and is fairly simple to do. The second is an exit command.
The last bit is how I actually execute the input command, provided it isn't one of the special cases mentioned above. The are a couple of ways I could have done it. The first would be using the System.Diagnostics.Process class, invoking an instance of cmd.exe, and passing the command to that. As mentioned before, the Command Line is blocked on the school computers, so I wasn't sure that would work. Another alternative was writing the command to a batch file, then executing it. However, I wanted a simpler way to do it, one without using the Process class.
This leads to the system method. C++ programmers will recognize it - it's most common use seems to be 'system("pause")'. It's general purpose is to run commands. The P/Invoke definition for it is as such:
[source lang=csharp]
[DllImport("msvcrt.dll", SetLastError = true)]
public static extern void system(string command);
[/source]
It's simple, and works wonderfully. All output is properly redirected to the program's console.
Now for the kicker - easily adding values to the system path. To do this, I defined a method:
[source lang="csharp"]
private static void AddToSystemPath(string path)
{
var current = Environment.GetEnvironmentVariable("PATH");
Environment.SetEnvironmentVariable("PATH", path + ";" + current);
}
[/source]
And modified the main if-block:
[source lang=csharp]
if (input.Length >= 2 && input.Substring(0, 2).ToLower() == "cd")
ChangeDirectory(input);
else if (input.ToLower() == "exit")
break;
else if (input.Length > 8 && input.Substring(0, 8).ToLower() == "addpath ")
AddToSystemPath(input.Split(new[] { ' ' }, 2)[1]);
else
system(input);
[/source]
Voila. A giant mess. Look at all those random numbers, those nonsensical lines of code without even a comment. Time to clean up...
...except I never got to that. I had a better idea. A lot of my development tools are portable - I could stick them on my USB stick. Why don't I make an application which can give me easy access to all my applications and files, allow me to easily edit the system path, and provide me a command prompt? And so, I ditched my makeshift command line for this monstrosity, the aptly named Flash Centre:
Hey look, I finally learned some WPF!!
Next post, I'll describe how I managed to emulate a command prompt in a textbox, and the various issues I had to put up with.
YellPika
EDIT: Just noticed that some things did not paste properly into the post. I believe everything is fixed now.
Way to go, computer admin guys. Bet you've seen Swordfish and everything.