Friday, December 3, 2010

C# Console Application

If you are using someone else's computer that doesn't have the C# editor installed, you can still write C# code and run it!  The answer is a Console Application.  (You can also create a Console Application in the the C# editor by clicking New Project, and then selecting Console Application)


Important stuff will be in red.


Basically, what you need is a simple text editor (I recommend the newest version of Notepad++ because it has syntax highlighting like your normal editor).  You can put all your code into the text editor and save it as a .CS file, which will look something like this:

using System;
using System.Collections.Generic;
using System.Text;


class ClassName
{


static void Main()
{

}
}


You put all of your code in the Main() method because that's what runs when you start the program.  You can add more namespace references (the "using ... " statements at the top) as needed, such as System.Drawing.  You can add other methods in the class if you want, just like normal.  You can even add new classes -- just make sure that the end-brace of one class is outside the starting-brace of the next class.


The following are 3 main commands that you will use to interact with the user:
  • Console.Write(string text);
    • This outputs the text in the console window.
  • Console.WriteLine(string text);
    • This outputs the text in the console window and also puts a line break after, so whatever comes next will be on a new line.  I think this is more useful overall than Console.Write(...).
  • Console.ReadLine();
    • This gets user input from the user and reads in everything the user types in until he/she presses [ENTER].
    • It also halts program execution until the user presses [ENTER], so it is quite useful for this to be the last line in your Main() method so that it will pause at the end of the program so you can read the output from your program before hitting [ENTER] to terminate it.
You still need to compile your code, so you should use a handy program that comes with your computer: the C# compiler.  Using your text editor, put the following code into it and save it as a .BAT file:

C:\Windows\Microsoft.NET\Framework\v3.5\csc.exe "ConsoleApp.cs"
pause
"ConsoleApp.exe"

The first line of code is the most important because it runs the C# compiler and compile "ConsoleApp.cs".  You should change "ConsoleApp.cs" to whatever filename you chose (if your filename has a space character in it, you have to keep the quotes).  Note: if you have different versions of the .NET framework installed, I recommend you use the latest version.  You can replace "v3.5" with "v4.0.30319" or "v2.0.50727".  You can check your versions of your .NET framework here (they're the folders that start with "v"):
C:\Windows\Microsoft.NET\Framework.


The next line tells the console to pause so you can see what happened during the compilation and so that you can fix any errors that were found.


The third line runs your application that is created if the compile was successful (or the last successful build if it failed).  Again, change "ConsoleApp.cs" to the filename you chose for your C# source code file.


Run this .BAT file to compile and run your new console application!


You can download the entire bunch of files here (ConsoleApp.zip) including a template for your Console Application and the batch file to compile it.  The numbers in the .BAT files represent the version of .NET that it's going to try to compile with.  I recommend you try them from latest (v4.0.30319) to oldest (v2.0.50727) and use the newest .NET version that works.  Remember to rename all instances of "ConsoleApp.cs" to whatever you want to name your C# source code file.  Just another note: I like to name the compilers "c_ConsoleApp.bat" just so I know which .BAT file will compile which .CS file.  It's not mandatory, but I find it helpful when there's a lot of files in one folder.

No comments:

Post a Comment