User login

How Do I Get The C# Compiler To Work From The Command Line In Any Directory?

Thanks to TJ for contributing this to the program-l e-mail group.

You are getting the message:

csc.exe is not recognized as an internal or external command, operable program or a batch file...

All this message really means is that the system cannot find the program requested, because it is not in the current path of directories it checks.

There are many ways to add a directory to the path. I will show you the one that I use.

Step 1: open a command prompt. To do this, hold down the Windows key and press the letter R on systems older than Vista. In Vista and newer, hit the Windows key. Once you have an edit box focused, type cmd and press Enter.

At the command line, enter the command: cls

This will clear the screen and make it easier to work with later.

Step 2: enter cd \

This will take you to the root directory of drive c:

To verify this, look at the bottom of the screen. It should look something like: c:\>

C:\> is the working command prompt.

Step 3: enter md cSharp

This will create a working directory called cSharp. Md stands for make directory.
We will use this directory to store the results of some later commands.

Step 4: enter dir /s csc.exe

Dir is the dos directory command. It will give a listing of the files in a directory.
/s is an argument telling the dir command to search the current directory and all subdirectories for a given file.

csc.exe - is the c# compiler. In our case it is the requested file.

If you look at the screen, you will see a listing of all of the directories that have a csc.exe file within the current working directory, which is the root.

It is hard to work with the data that is on the screen. The next step will save the important data to a file that will make it easier to work with.

Step 5: enter dir /s /b csc.exe > c:\cSharp\csc.lst

This is the same command that you entered in step 4, but the results are now
reduced to the bare minimum by using the /b argument and being directed or "piped" to a file named csc.lst. That file is in the working directory that you created earlier: c:\cSharp

Step 6: enter cd cSharp

This means change directory. Now the working directory is c:\CSharp.

Your prompt should now look something like c:\cSharp>

Step 7: open the file csc.lst with your favorite text editor

The path to the file is: c:\cSharp\csc.lst

One thing you could do is type: start notepad csc.lst. This will start notepad and load the csc.lst file.

Note: there are different versions of the ms .net framework on your system
Each version will contain a csc.exe file. I choose to use the latest version on my system. That was:

c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe

Step 8: remove the filename: csc.exe from the directory path that points to the C# compiler you want to use.

Using your text editor, remove the "\csc.exe" from the end of the ms .net frame work path.

Your line should look something like this:

c:\Windows\Microsoft.NET\Framework\v4.0.30319

This is the directory that needs to be added to the system path.

Copy this line to the clipboard.

Save the file for further use.

Step 9: Go back to the command line and enter echo %path%

This will display your current path. It is to this path that the results of step 9 must be added.

Step 10: open your text editor to a new file and enter the following:

@echo off
rem add2path.cmd
rem add to the current path
rem
set old_path = %path%
set path = %old_path%;

** Note:
Paste the copied line to the set path statement above after the ;

The path statement should look something like this:

set path = %old_path%;c:\Windows\Microsoft.NET\Framework\v4.0.30319

Add this line to the file:

echo %path%

Save the file as: add2path.cmd

Step 11: review the batch file you created. It should look similar to this:

@echo off
rem add2path.cmd
rem add to the current path
rem
set old_path = %path%
set path = %old_path%;c:\Windows\Microsoft.NET\Framework\v4.0.30319
echo %path%

Step 12: From the command line, enter add2path.cmd

You should see the current path, which should now include the added .net frame work directory

Step 13: enter cls to clear the screen

Step 14: enter csc.exe

If all goes well, you should see the options that can be used with the csc.exe compiler.

You should now be able to run csc.exe from any of your directories without getting the dreaded error:

Csc.exe is not recognized as an internal or external command, operable program or a batch file...

Book navigation