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 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.
Another way to compile C# programs (without using Visual Studio or without having it installed) is to create a user variable in environment variables, namely "PATH".
Copy the following path in this variable:
"C:\Windows\Microsoft.NET\Framework\v4.0.30319"
or depending upon which .NET your PC have.
So you don't have to mention the whole path every time you compile a code. Simply use
"C:\Users\UserName\Desktop>csc [options] filename.cs"
or wherever the path of your code is.
Now you are good to go.
Here is how to install MSBuild with standalone C# 7.0 compiler which is no longer bundled in the latest .Net Framework 4.7:
Is it possible to install a C# compiler without Visual Studio?
Then just run
"C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\Roslyn\csc.exe" MyApplication.cs
to compile single source file to executable.
Also note that .Net Core doesn't support compiling single source file without preconfigured project.
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 isgmcs
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.