在命令提示符中编译/执行 C# 源文件 [英] Compiling/Executing a C# Source File in Command Prompt

查看:34
本文介绍了在命令提示符中编译/执行 C# 源文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

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

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,

例如对于 .NET 3.5,c:windowsMicrosoft.NETFrameworkv3.5.

e.g. for .NET 3.5, c:windowsMicrosoft.NETFrameworkv3.5.

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

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.

像这样运行 C# 编译器:

Run the C# compiler like this:

  c:windowsMicrosoft.NETFrameworkv3.5incsc.exe 
            /t:exe /out:MyApplication.exe MyApplication.cs  ...

(全部在一行)

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

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 .

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

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

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

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

这篇文章在 MSDN 上更详细地了解命令行编译器的选项.您可以嵌入资源、设置图标、签署程序集 - 您可以在 Visual Studio 中执行的所有操作.

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.

如果你安装了 Visual Studio,在开始菜单"中;在 Visual Studio 工具下,您可以打开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.

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

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.

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

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

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

然后运行它:

$ mono MyApplication.exe

这篇关于在命令提示符中编译/执行 C# 源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆