在命令提示符中编译/执行 C# 源文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/553143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 07:54:17  来源:igfitidea点击:

Compiling/Executing a C# Source File in Command Prompt

c#command-linecompilationcommand-prompt

提问by DragonBorn

How do you compile and execute a .cs file from a command-prompt window?

如何从命令提示符窗口编译和执行 .cs 文件?

采纳答案by Ash

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,

CSC.exe 是 .NET Framework 中包含的 CSharp 编译器,可用于从命令提示符进行编译。输出可以是一个可执行文件“.exe”,如果你使用“/target:exe”,或者一个 DLL;如果使用 /target:library,CSC.exe 会在 .NET Framework 目录中找到,

e.g. for .NET 3.5, c:\windows\Microsoft.NET\Framework\v3.5\.

例如,对于.NET 3.5 c:\windows\Microsoft.NET\Framework\v3.5\

To run, 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.

要运行,首先打开命令提示符,单击“开始”,然后键入cmd.exe.
然后,您可能必须 cd 进入保存源文件的目录。

Run the C# compiler like this:

像这样运行 C# 编译器:

  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.

如果您有多个要编译的源模块,您可以将其放在同一命令行中。如果您有其他程序集要引用,请使用/r:AssemblyName.dll.

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

确保您在其中一个类中定义了静态 Main() 方法,作为“入口点”。

To run the resulting EXE, type MyApplication, followed by <ENTER>using the command prompt.

要运行生成的 EXE,请键入MyApplication,然后<ENTER>使用命令提示符。

This articleon 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.

MSDN 上的这篇文章更详细地介绍了命令行编译器的选项。您可以嵌入资源、设置图标、签署程序集 - 您可以在 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.

如果你安装了 Visual Studio,在“开始菜单”中;在 Visual Studio 工具下,您可以打开“Visual Studio 命令提示符”,它将为命令行编译设置所有必需的环境和路径变量。

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, FinalBuilderetc. These tools provide a complete build environment, not just the basic compiler.

虽然了解这一点非常方便,但您应该将其与某种构建工具(如NAntMSBuildFinalBuilder等)的知识相结合。这些工具提供了一个完整的构建环境,而不仅仅是基本的编译器。

On a Mac

在 Mac 上

On a Mac, syntax is similar, only C sharp Compiler is just named csc:

在 Mac 上,语法类似,只是 CSharp Compiler 只是命名为csc

$ csc /target:exe /out:MyApplication.exe MyApplication.cs ...

Then to run it :

然后运行它:

$ mono MyApplication.exe

回答by Cerebrus

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.

您可以在 VS 命令提示符中构建您的类文件(以便加载所有必需的环境变量),而不是默认的 Windows 命令窗口。

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

要了解有关使用 csc.exe(编译器)构建命令行的更多信息,请参阅此文章

回答by Marc Gravell

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 editionis free and very good for the money ;-p

虽然知道如何在命令行中构建绝对是一件好事,但对于大多数工作来说,使用 IDE 可能更容易。C#速成版是免费的,而且物有所值;-p

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

或者,可以使用snippy 之类的东西来运行 C# 代码片段。

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

最后 - 请注意,命令行是特定于实现的;对于 MS,它是csc;对于mono,它是gmcs和朋友......同样,要执行:对于 MS 版本,它只是“exename”,但对于 mono 来说通常是“mo​​no exename”。

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

最后,很多项目都是用构建脚本工具构建的;MSBuildNAnt等。

回答by nullsteph

LinqPad is a quick way to test out some C# code, and its free.

LinqPad 是测试一些 C# 代码的快速方法,而且它是免费的。

回答by Aditya Garg

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".

另一种编译 C# 程序的方法(不使用 Visual Studio 或不安装它)是在环境变量中创建一个用户变量,即“PATH”。

Copy the following path in this variable:

复制此变量中的以下路径:

"C:\Windows\Microsoft.NET\Framework\v4.0.30319"

“C:\Windows\Microsoft.NET\Framework\v4.0.30319”

or depending upon which .NET your PC have.

或取决于您的 PC 具有哪个 .NET。

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"

“C:\Users\UserName\Desktop>csc [选项] 文件名.cs”

or wherever the path of your code is.

或者你的代码路径在哪里。

Now you are good to go.

现在你可以走了。

回答by Surendra

You can compile a C# program :

你可以编译一个 C# 程序:

c: > csc Hello.cs

c: > csc Hello.cs

You can run the program

你可以运行程序

c: > Hello

c: > 你好

回答by Joe

Once you write the c# code and save it. You can use the command prompt to execute it just like the other code.

一旦你编写了 c# 代码并保存它。您可以使用命令提示符来执行它,就像其他代码一样。

In command prompt you enter the directory your file is in and type

在命令提示符中,您输入文件所在的目录并键入

To Compile:

编译:

mcs yourfilename.cs

To Execute:

执行:

mono yourfilename.exe 

if you want your .exe file to be different with a different name, type

如果您希望 .exe 文件具有不同的名称,请键入

To Compile:

编译:

mcs yourfilename.cs -out:anyname.exe 

To Execute:

执行:

mono anyname.exe 

This should help!

这应该有帮助!

回答by DeepSpace101

For the latest version, first open a Powershell window, go to any folder (e.g. c:\projects\) and run the following

对于最新版本,首先打开一个 Powershell 窗口,转到任何文件夹(例如c:\projects\)并运行以下命令

# Get nuget.exe command line
wget https://dist.nuget.org/win-x86-commandline/latest/nuget.exe -OutFile nuget.exe

# Download the C# Roslyn compiler (just a few megs, no need to 'install')
.\nuget.exe install Microsoft.Net.Compilers

# Compiler, meet code
.\Microsoft.Net.Compilers.1.3.2\tools\csc.exe .\HelloWorld.cs

# Run it
.\HelloWorld.exe    

An example HelloWorld.cs

一个例子 HelloWorld.cs

using System;

public class HelloWorld {
    public static void Main() 
    {
        Console.WriteLine("Hello world!");
    }
}

You can also try the new C# interpreter ;)

您还可以尝试新的 C# 解释器;)

.\Microsoft.Net.Compilers.1.3.2\tools\csi.exe
> Console.WriteLine("Hello world!");
Hello world!

回答by Vadzim

Here is how to install MSBuild with standalone C# 7.0 compiler which is no longer bundled in the latest .Net Framework 4.7:

以下是如何使用独立的 C# 7.0 编译器安装 MSBuild,该编译器不再捆绑在最新的 .Net Framework 4.7 中:

Is it possible to install a C# compiler without Visual Studio?

是否可以在没有 Visual Studio 的情况下安装 C# 编译器?

Then just run

然后就跑

"C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\Roslyn\csc.exe" MyApplication.cs

"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 filewithout preconfigured project.

另请注意,.Net Core不支持在没有预配置项目的情况下编译单个源文件

回答by user3789482

In Windows systems, use the command csc <filname>.csin the command prompt while the current directory is in Microsoft Visual Studio\<Year>\<Version>

在 Windows 系统中,csc <filname>.cs当当前目录在 Microsoft Visual Studio\<Year>\<Version> 中时,使用命令提示符中的命令

There are two ways:

有两种方式:

Using the command prompt:

使用命令提示符:

  1. Start --> Command Prompt
  2. Change the directory to Visual Studio folder, using the command: cd C:\Program Files (x86)\Microsoft Visual Studio\2017\ <Version>
  3. Use the command: csc /.cs
  1. 开始 --> 命令提示符
  2. 使用以下命令将目录更改为 Visual Studio 文件夹:cd C:\Program Files (x86)\Microsoft Visual Studio\2017\ <Version>
  3. 使用命令:csc /.cs

Using Developer Command Prompt :

使用开发人员命令提示符:

  1. Start --> Developer Command Prompt for VS 2017 (Here the directory is already set to Visual Studio folder)

  2. Use the command: csc /.cs

  1. Start --> Developer Command Prompt for VS 2017(这里的目录已经设置为Visual Studio文件夹)

  2. 使用命令:csc /.cs

Hope it helps!

希望能帮助到你!