How do you compile and execute a .cs file from a command-prompt window?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
You can compile a C# program :
c: > csc Hello.cs
You can run the program
c: > Hello
In Windows systems, use the command
csc <filname>.cs
in the command prompt while the current directory is in Microsoft Visual Studio\<Year>\<Version>There are two ways:
Using the command prompt:
Using Developer Command Prompt :
Start --> Developer Command Prompt for VS 2017 (Here the directory is already set to Visual Studio folder)
Use the command: csc /.cs
Hope it helps!
For the latest version, first open a Powershell window, go to any folder (e.g.
c:\projects\
) and run the followingAn example HelloWorld.cs
You can also try the new C# interpreter ;)
LinqPad is a quick way to test out some C# code, and its free.
Once you write the c# code and save it. You can use the command prompt to execute it just like the other code.
In command prompt you enter the directory your file is in and type
To Compile:
To Execute:
if you want your .exe file to be different with a different name, type
To Compile:
To Execute:
This should help!
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, e.g. 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:
(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
MyApplication
followed by<ENTER>
at the command prompt.This article on MSDN goes into 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.
On a Mac
On a Mac, syntax is similar, only C sharp Compiler is just named
csc
:Then to run it :