ARTICLE

Creating C# Console Application using a Text Editor

Posted by Sandeep Shekhawat Articles | Visual C# June 03, 2011
Here I show steps to create console application using a text editor and compiling from a command window.
Reader Level:

We can develop a C# standalone program or console application using an Integrate Development Environment (IDE) like Visual Studio or using a plain text editor like Notepad, Wordpad etc. The following shows how to do that using a text editor for a simple "Hello World" program. First of all set the path for the C# Compiler (using Windows 7).

1. Right-click Computer and click on Properties from Drop-Down Pane.

2. Now Control Panel Home open and click on Advanced system settings.

texteditor1.jpg 
 
Fig 1. Control Panel Home

3. A new window System Properties opens. Click on the Advanced Tab and click on Environment Variables. 

texteditor2.jpg
  
Fig 2 System Properties

4. A new window for Environment Variables opens. 

texteditor3.jpg
 
Fig 3 Environment Variable

5. There are two parts:
  • User variables
  • System variables    
Under the User variables Pane click on the New button. A window New User Variable opens; it has two parameters:
  • Variable name 
  • Variable value
In the Variable name write path. For the Variable value write the C# compiler path, such as: 

C:\Windows\Microsoft.NET\Framework64\v3.5

texteditor4.jpg
 
Fig 4 New User Variable

6. In the New User Variable Window click the OK button then for the Environment Variables window click the OK button then for the System Properties window click the OK button.

7. Open a Command Prompt and type in the command csc and press ENTER.

If following prompt appears then that means your C# compiler 3.5 path is correct.

texteditor5.jpg 

Fig 5 Path Varification

8. Type the following code with any Text Editor (Notepad, WordPad, etc):

class HelloWorld
 {
static void Main()
 {
System.Console.Write("Hello World");
 }
  }

9. Save the file at any location of the hard drive with class name and .cs extension. Open a Command Prompt window and compile HelloWorld.cs class using following command:

csc file-name 

for example csc HelloWorld.cs 

After successful compilation the HelloWorld.cs file's executable file HelloWorld.exe is created; now run this .exe file using the following command:

filename

for example HelloWorld.exe

Or 

HelloWorld

Now our output is 

texteditor6.jpg 

Fig 6 Output

Login to add your contents and source code to this article
Article Extensions
Contents added by Suthish Nair on Jun 04, 2011

For Windows XP users...

C:\Windows\Microsoft.NET\Framework64\v3.5 ==> C:\WINDOWS\Microsoft.NET\Framework\v3.5

Point 7. After choosing above directory through command prompt then enter csc

Point 9. Save to a folder. e.g. C:\HelloWorld.cs

    Now back to command prompt. Run the csc command with File path.
      
            e.g. C:\WINDOWS\Microsoft.NET\Framework\v3.5> csc C:\HelloWorld.cs

    Now open the folder C:\WINDOWS\Microsoft.NET\Framework\v3.5 you can see HelloWorld.exe.

            Double Click on it for output. Remember to have System.Console.ReadLine();.


Thank You !

   

post comment
     

Thank You For Update

Posted by Sandeep Shekhawat Jun 04, 2011

Thanks for sharing. I also updated the article with Article Extensions.

Posted by Suthish Nair Jun 04, 2011

Thank You Sam. It is an Educational article for beginners in development. It is not a way to develop whole application. When beginner use IDE directly then he/she could not remember syntax and terminology of c# due to intellisense of IDE. So prefer always IDE to develop an application.

Posted by Sandeep Shekhawat Jun 03, 2011

Another educational article would be to show how to use MSBuild. Note that it is possible to compile multiple C# files and get them linked together but I am not sure of the details. The .Net tool MSBulld is used to control the build. When Visual Studio does a build it uses MSBuild to do it. Most of the input to MSBuild come from XML files. A very simple IDE could be created that allows minimal editing of the MSBuild data and that issues the MSBuild command and redirects the output to an output window. That are the fundamentals of what VS does.

Posted by Sam Hobbs Jun 03, 2011

This will be educational for people unfamiliar with the old-fashioned way of doing things. I want to suggest however that instead of setting the environment variables permanently it is possible to set them temporarily for a specific command window using vcvars32.bat. For simple educational purposes such as this it is more efficient to use vcvars32.bat and it is a worthwhile education to know how to do that. The vcvars32.bat is in a folder such as: C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin. Yes I know that is the folder for C++. There might be a more appropriate bat file for .Net. It would be extremely easy to create a bat file for this.

Posted by Sam Hobbs Jun 03, 2011
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter