The problem with this is that anyone who opens Visual Studio for the first time and isn't already an expert developer will be immediately lost in the sea of options, buttons, and menu items that VS has to offer. At least that is what happened, and continues to happen, to me! Luckily, there is an easy, non-bloated way to compile C# programs that bypasses Visual Studio altogether. Just make sure you have the .NET Framework installed. You can download it here: http://www.microsoft.com/net
Command Line to the Rescue!
It turns out that the Microsoft.NET Framework (which is the backbone of Visual Studio) uses a simple executable called csc.exe to compile C# programs. So, if you've written a C# program and saved it with the filename "myprogram.cs", you can compile that program from the command line using csc.exe. The syntax would look something like this:csc myprogram.cs
The output will be an executable file named myprogram.exe.
Now, if you typed the above command into your command line, you probably noticed that it didn't work and you got an error telling you something about 'csc' not being recognized as a command. This is because you'll need to specify the path to csc.exe on your computer before you can run it. On my machine, csc.exe lives in the C:\Windows\Microsoft.NET\Framework\v3.5\ directory. Thus, the command to compile myprogram.cs would be:
C:\Windows\Microsoft.NET\Framework\v3.5\csc myprogram.cs
NOTE: I'm using version 3.5 of the .NET Framework for these examples. Be sure to use the correct path for whichever version you have installed.
You can also specify an alternate location or filename for the output (.exe) file using the /out parameter. For example:
C:\Windows\Microsoft.NET\Framework\v3.5\csc /out:mycustomfilename.exe myprogram.cs
You can even use the * wildcard to compile multiple .cs files in one sweep:
C:\Windows\Microsoft.NET\Framework\v3.5\csc *.cs
Environment Variables make it Easier
If you're going to be developing C# applications, you're going to be compiling and running your programs very frequently and you're probably going to quickly tire of typing the full path to csc.exe each time. Not to fear! You can set up a system environment variable to make csc.exe accessible from any directoryNOTE: You will need Administrator privileges for the following steps.
To set up a system environment variable in Windows 7:
- Click on "Start" and right-click on "Computer"
- Click "Properties"
- Click the "Advanced system settings" link (on the left)
- Click the "Environment Variables..." button (at the bottom)
- Under the "System variables" section single-click (highlight) the "Path" item.
- Click "Edit..."
- Append the text
;C:\Windows\Microsoft.NET\Framework\v3.5\
to the "Variable value" field. Note the semicolon (;) and be very careful not to erase any of the existing system variables. All you're doing is adding another variable to the end of the list. - Click "OK" to save your edit.
Other Options
Sometimes when compiling, you will see various messages appear in the command line (error messages, warnings, etc.). If you need to save these messages for any reason, you can tell csc.exe to dump them into a .txt file by using the > operator. For example:csc myprogram.cs > filename.txt
Also, if you're not a command line type of a person, you could always create a batch file to handle the command line stuff for you by typing the necessary compilation commands (see above) into a .txt file and saving it with a .bat extension. The resulting file (filename.bat) should be placed in the same folder as the .cs files that you with to compile. When double-clicked, the batch file will execute the commands it contains exactly as if they had been typed in the command line. More about batch files here: http://www.robvanderwoude.com/batchfiles.php