views:

1524

answers:

3

Pardon my ignorance but how do you compile a .CS file from a command-prompt window? And how do you execute?

+15  A: 

CSC.exe is the CSharp compiler included in the .NET Framework and can be used to compile from the command prompt. The output can be an executable (.exe) if you use /target:exe, or a DLL if you use /target:library. CSC.exe is found in the .NET Framework directory, eg for .NET 3.5, c:\windows\Microsoft.NET\Framework\v3.5\.

To run it, first open a command prompt. click Start... then type cmd.exe. You may then have to cd into the directory that holds your source files.

Run the C# compiler like this:

  c:\windows\Microsoft.NET\Framework\v3.5\bin\csc.exe 
            /t:exe /out:MyApplication.exe MyApplication.cs  ...

(all on one line)

If you have more than one source module to be compiled, you can put it on that same command line. If you have other assemblies to reference, use /r:AssemblyName.dll .

Ensure you have a static Main() method defined in one of your classes to act as the "entry point".

To run the resulting EXE, just type MyApplicaction followed by <ENTER> at the command prompt.

This article on MSDN goes in to more detail on the options for the command-line compiler. You can embed resources, set icons, sign assemblies - everything you could do within Visual Studio.

If you have Visual Studio installed, in the Start menu (under Visual Studio Tools) you can open a "Visual Studio Command Prompt" that will set up all required environment and path variables for command line compilation.

While it's very handy to know of this, you should combine it with knowledge of some sort of build tool such as NAnt, MSBuild, FinalBuilder etc. These tools provide a complete build environment, not just the basic compiler.

Ash
if I remember correctly, you can only compile to and exe if you have a static class with a Main method.
ck
The compiler warns you these days if you haven't, but good point I'll add that to the answer, thanks.
Ash
+2  A: 

You can build your class files within the VS Command prompt (so that all required environment variables are loaded), not the default Windows command window.

To know more about command line building with csc.exe (the compiler), see this article.

Cerebrus
+3  A: 

While it is definitely a good thing knowing how to build at the command line, for most work it might be easier to use an IDE. The C# express edition is free and very good for the money ;-p

Alternatively, things like snippy can be used to run fragments of C# code.

Finally - note that the command line is implementation specific; for MS, it is csc; for mono, it is gmcs and friends.... Likewise, to execute: it is just "exename" for the MS version, but typically "mono exename" for mono.

Finally, many projects are build with build script tools; MSBuild, NAnt, etc.

Marc Gravell
I do use the IDE. But I needed to know. It just doesn't feel right not knowing! Thanks a lot for your response.
ZiG
+1 for NAnt! LINQPad is very good for experimenting with code fragments: http://www.linqpad.net/
TrueWill