A.3. Odds and Ends Tips to Get C# to Work Properly

After you get the .NET Framework 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.

A.3.1. If You're Working Under Windows 2000 or Older Operating Systems

As of the time of publication of this book (fall 2008), the latest version of the .NET Framework is designed to work with Windows Vista, Windows XP, Windows Server 2003, and Windows Server 2008. If you're running on an older operating system (Windows 2000, Windows Me, and so on), you should seriously consider buying a new computer. If that's not practical, you can still install older versions of the .NET Framework on your computer, but you won't have access to more recently added features or language elements.

A.3.2. If You're Working Under UNIX (Solaris, Linux)

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.

A.3.3. Setting the Path Environment Variable

When the .NET Framework is installed, all associated files will be placed in a specific directory. 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 that can be accessed through the System icon in the Control Panel. Edit the Path environment variable and add the path to the C# compiler to the end of the string, preceded by a semicolon. A typical C# compiler path looks like the following:

C:WINDOWSMicrosoft.NETFrameworkv3.5

You should now be able to invoke the C# compiler from anywhere on your machine.

NOTE

The manner in which these steps are accomplished might differ for other versions of Windows.

A.3.4. Once the .NET Framework Is Installed and the Path Variable Is Set

After you have successfully installed the .NET Framework 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 B for download instructions).

Don't put any of your personal files or folders in the .NET Framework home directory or any of its subdirectories unless specifically instructed to do so.


Your C# environment should now 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 the Windows Notepad editor.

  • Use any other Windows-based text editor that you prefer.

  • Use your favorite command-line text editor.

  • Use an integrated development environment (IDE) of your choice; Visual Studio is the Microsoft standard.

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.

  • From the Start menu, choose Run, and then type cmd.

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!

A.3.5. Troubleshooting Your Installation

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, the C# compiler could not be found:

    C:MyDir> csc Success.cs
    'csc' is not recognized as an internal or external command, operable
    program, or batch file.

    You either improperly installed the .NET Framework or did not properly update the Path environment variable.

  • If you get the following error message when attempting to compile, the computer can't find your source code:

    C:MyDir> csc Success.cs
    error: Source file 'Success.cs' could not be found

    Make sure that you're in the correct directory where the program source code file resides and that you're spelling the name of the file correctly.

  • If you get any other compilation errors, such as the following, check to make sure that you've typed in the program exactly as shown previously:

    C:MyDir> csc Success.cs
    Success.cs (5,23) error: Newline in constant Console.WriteLine("Hooray!);

    In this particular example, the double quote mark at the end of "Hooray!" is missing.

  • If you get the following error message when attempting to run a successfully compiled program, it can mean one of several different things:

    C:MyDir> Success
    'Success' is not a recognized internal or external command, operable
    program, or batch file

    You might be spelling the name of the program incorrectly (again, pay attention to uppercase/lowercase), or the program didn't compile correctly, thus failing to produce a Success.exe file, or you're trying to run the program from the wrong directory.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset