I’m not sure if this question has been answered elsewhere and I can’t seem to find anything through google that isn’t a “Hello World” example… I’m coding in C# .NET 4.0.
I’m trying to develop a console application that will open, display text, and then wait for the user to input commands, where the commands will run particular business logic.
For example: If the user opens the application and types “help”, I want to display a number of statements etc etc. I’m not sure how to code the ‘event handler’ for user input though.
Hopefully this makes sense. Any help would be much appreciated!
Cheers.
You need several steps to achieve this but it shouldn’t be that hard. First you need some kind of parser that parses what you write. To read each command just use
var command = Console.ReadLine(), and then parse that line. And execute the command… Main logic should have a base looking this (sort of):Sort of, you could probably change that to be more complicated.
The code for the
Parserand command is sort of straight forward: