Easy CBASIC Lesson 8 How to create and compile C# Programs in Windows

of Acoptex.com in Csharp

CBASIC Learning C# basics

Tags: C#, Csharp, C# fundamentals, C# basics

These lessons are designed to keep you engaged with the process of learning C# basics. Our main focus will be on learning C# basics. 

Lesson 8 C# How to create and compile C# Programs in Windows

In this lesson we will show you how to create and compile C# programs in Windows.

1. How to create C# programs in the Windows

First we start the Windows command console, also known as CommandPrompt. In Windows 7 this is done from the Windows Explorer start menu:

Start -> Programs -> Accessories -> Command Prompt

It is advised that we run the console as administrator (right click on the Command Prompt icon and choose "Run as administrator"). Otherwisesome operations we want to use may be restricted.

There are other ways to do it - find the cmd.exe file (C:\Windows\System32\cmd.exe).

In Windows 10: Search Windows ->Type cmd

After opening the console, let’s create a directory, in which we will experiment. We use the md command to create a directory and cd command to navigate to it (enter inside it):

The directory will be named IntroCSharp and located in C:\. We change the current directory to C:\IntroCSharp and create a new file HelloCSharp.cs, by using the built-in Windows text editor – Notepad. To create the text file we execute the following command on the console: notepad HelloCSharp.cs. This will start Notepad with the following dialog window, confirming the creation of a new file:

Notepad will warn us that no such file exists and will ask us if we want to create it. We click on Yes button. The next step is to rewrite or simply Copy / Paste the program’s source code.

using System

class HelloCSharp


static void Main()


Console.WriteLine("Hello C#!");



We save it by pressing Ctrl+S and close the Notepad editor with Alt+F4. We have the initial code of our sample C# program now, written in the file C:\IntroCSharp\HelloCSharp.cs

2. How to compile C# programs in the Windows

Compiling is done by the csc.exe compiler.

We got an error – Windows cannot find an executable file or command with the name "csc". This is a very common problem and it is normal to appear if it is our first time using C#. Several reasons might have caused it:

  • The .NET Framework is not installed;
  • The .NET Framework is installed correctly, but its directory Microsoft.NET\Framework\v4.0.xxx is not added to the system path for executable files and Windows cannot find csc.exe.

The first problem is easily solved by installing the .NET Framework (in our case – version 4.5). The other problem can be solved by changing the system path (we will do this later) or by using the full path to csc.exe, as it is shown on the figure below. In our case, the full file path to the C# compiler is C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe (note that this path could vary depending on the .NET framework version installed). 

After the execution csc is completed without any errors, and we get the following file as a result: C:\IntroCSharp\HelloCSharp.exe. To run it, we simply need to write its name. The result of the execution of our program is the message "Hello, C#!" printed on the console.

3. Changing the System Paths in Windows

If we want to use the csc.exe without entering the full path to it, we could add its folder to the Windows system path.

  1. Open Control Panel and select System
  2. We select Advanced system settings. The dialog window System Properties appears:
  3. We click on button Environment Variables and a window with all the environment variables shows up.
  4. We choose Path from the list of System variables, as shown on the figure, and press the Edit button. A small window appears, in which we enter the path to the directory where the .NET Framework is installed:
  5. Of course, first we need to know where .NET Framework is installed. By default it is located somewhere inside the Windows system directory C:\Windows\Microsoft.NET, for example: C:\Windows\Microsoft.NET\Framework64\v4.0.30319. Adding the additional path to the already existing ones in the Path variable of the environment is done by adjoining the path name to the others and using a semicolon (;) as a spacer.
  6. We must be careful because if we delete any of the existing system paths, some of Windows’ functions or part of the installed software might fail to operate properly.
  7. When we are done with setting the path, we can try running csc.exe, without entering its full path. To do so, we open a new Command Prompt window (it is important to restart the Command Prompt) and type in the "csc" command. We should see the C# compiler version and a message that no input file has been specified:

Next -> CBASIC Lesson 9 Compiling and debugging in Visual Studio

Other projects of Acoptex.com
Medium Basics: Project 083w Sipeed Maixduino board - Using PlatformIO IDE of Acoptex.com in Sipeed Maixduino 08-08-2019
Medium Basics: Project 083e Sipeed Maixduino board - Uploading MaixPy of Acoptex.com in Sipeed Maixduino 04-08-2019
Medium Basics: Project 083f Sipeed Maixduino board - Using MycroPython of Acoptex.com in Sipeed Maixduino 04-08-2019

Published at 09-04-2019
Viewed: 1199 times