Emulating the Command Prompt in C#

Published August 07, 2011
Advertisement
Mirrored from my blog.

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:
image.png
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.
0 likes 5 comments

Comments

Aardvajk
I love the fact that your school disables the command prompt, but allows you to execute programs that can do things like change the system path.

Way to go, computer admin guys. Bet you've seen Swordfish and everything. :)
August 07, 2011 08:02 AM
zarfius
Ha! Reminds me of when I was in school. It's almost as if they put it there just to challenge you ;)
August 07, 2011 10:46 AM
YellPika
@Aardvajk Well, we can't [i]really[/i] change the system path - just for the current process, not the user or the machine. Considering that we do a lot from the command line, it was good enough.

The computers are a steaming pile of fail. They recently changed the system. All they succeeded in doing was a) making it harder (but not impossible) to use a computer and b) making the boot times 20x longer.
August 07, 2011 11:47 AM
?????????
your school disables the command prompt?!?! why?!?!
it's not like you can do anything harmful with a command prompt....
August 09, 2011 11:26 PM
YellPika
I think it's because, for some reason or another, they don't want us accessing the C drive. We can't access it though Windows Explorer. They've enabled the address bar in Windows Explorer, but prevent us from using it. We can't right click. We can't access file properties (before I was able to do it by clicking "Open File Location" in some shortcut properties). We can't access the Task Manager. We can't access the command prompt.

It is impossible to browse the C drive using any of the tools Windows normally provides, but nothing prevents us from using a different file browser. They even have Visual Studio installed - I made my own file browser when I had some spare time. I even made a makeshift Task Manager.

*Steaming Pile of Fail*
August 10, 2011 04:01 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement