After you get the .NET Framework SDK downloaded and installed per the instructions on Microsoft's web site, there are a few additional things you'll need to do to get C# up and running.
As mentioned earlier, the .NET Framework SDK only works for the Windows 2000, Windows XP, Windows Server 2003, or newer versions of Windows. If your machine is running an older version of Windows, you'll have to upgrade to a newer operating system before you can install the C# SDK.
The .NET Framework isn't presently available for the UNIX operating system, although a Linux version is reportedly being considered at the time of publication of this book. Check the Microsoft web site periodically for developments in this regard.
When the SDK is installed, all associated files will be placed in a specific directory. On machines running Windows XP, for example, the default installation directory will be
C:\WINDOWS\Microsoft.NET\Framework
To compile C# programs anywhere on your machine, you'll need to add the location of the folder that contains the C# compiler to the Path environment variable. The Path variable is a system variable, so you'll most likely need to be logged on with administrator privileges in order to make permanent changes to this variable.
To set the path to the C# compiler under Windows XP:
Click the Start button, select Control Panel, and then select System.
Select the Advanced tab and then click the Environment Variables button.
Highlight the Path system variable and click the Edit button.
Add the path to the directory containing the C# compiler, preceded by a semicolon, onto the end of the Path variable. For example, if the C# compiler (csc.exe) is located in the C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322 folder, you would add the text
;C:\WINDOWS\Microsoft.NET\Framework/v1.1.4322
(note the semicolon at the beginning of the string) to the end of the Path variable.
You should now be able to invoke the C# compiler from anywhere on your machine.
Note?/td> |
Note that the manner in which these steps are accomplished will differ for other versions of Windows. |
After you have successfully installed the .NET Framework SDK and have set the Path environment variable, you are ready to start developing C# programs! Create a working directory in which you plan to store your various C# experiments, and then download the example code for this book into that directory (see Appendix D for download instructions). Important: don't put any of your personal files or folders in the .NET Framework SDK home directory or any of its subdirectories unless specifically instructed to do so.
Your C# environment should now, hopefully, be up and running. To give it a test drive, type, compile, and run the following trivially simple program:
using System; public class Success { static void Main() { Console.WriteLine("Hooray! It works!"); } }
You must first enter the preceding program text exactly as shown into a file. The file name can be anything, but the convention is to name the file Success.cs. You can use a variety of methods to enter a C# program into a file:
Use any other Windows-based text editor that you prefer.
Use your favorite command-line text editor.
Use an interactive development environment (IDE) of your choice.
Next, we'll attempt to compile and run this program from the command line. You can open a command prompt window in one of two ways:
From the Start menu, choose Programs → Accessories → Command Prompt.
Alternatively, from the Start menu, choose Run, and then type "cmd" (without the quotes).
Once the command prompt window opens, make sure to use the cd command to switch your working directory to be the directory in which your program resides (if you aren't already there), and then type the following command at the prompt to compile the program:
csc Success.cs
If the program compiles without any errors, an executable Success.exe file will be created in the same directory where the Success.cs file resides. Type the following command at the command prompt to run the program:
Success
If all goes well, the following should appear as output:
Hooray! It works!
The following examples illustrate various problems that might arise when you try to compile a C# program:
If you get the following error message when attempting to compile:
C:\MyDir> csc Success.cs 'csc' is not recognized as an internal or external command, operable program, or batch file.
this means that the C# compiler could not be found; i.e., you've either improperly installed the .NET Framework SDK or not properly updated the Path environment variable.
If you get the following error message when attempting to compile:
C:\MyDir> csc Success.cs error CS2001: Source file 'Success.cs' could not be found
this means that the computer can't find your source code. Make sure that (a) you're in the correct directory where the program source code file resides and (b) you're spelling the name of the file correctly.
If you get any other compilation errors, for example:
C:\MyDir> csc Success.cs Success.cs (5,23) error CS1010: Newline in constant Console.WriteLine("Hooray!);
check to make sure that you've typed in the program exactly as shown previously. (In this particular example, we're missing the double quote mark at the end of "Hooray!")
If you get the following error message when attempting to run a successfully compiled program:
C:\MyDir> Success 'Success' is not a recognized internal or external command, operable program, or batch file
this could mean one of several different things: (a) you're spelling the name of the program incorrectly (again, pay attention to upper/lower case); (b) the program didn't compile correctly, thus failing to produce a Success.exe file; (c) you're trying to run the program from the wrong directory.