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

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

问题描述

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

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

推荐答案

CSC.exe是CSharp编译器包含在.NET Framework中,并且可以用于从命令提示符编译。如果您使用/ target:exe,输出可以是可执行文件(.exe),如果使用/ target:library,则输出为DLL。 CSC.exe是存放在.NET Framework目录中的,
例如对于.NET 3.5, c:\windows \Microsoft.NET \Framework\v3.5 \

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

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

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.

运行C#编译器,如下所示:

Run the C# compiler like this:

  c:\windows\Microsoft.NET\Framework\v3.5\bin\csc.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 .

要运行生成的EXE,只需输入 MyApplicaction

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

在MSDN上的本文介绍了有关命令行编译器选项的更多详细信息。你可以嵌入资源,设置图标,签署程序集 - 你可以在Visual Studio中做的一切。

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.

如果您安装了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.

虽然它很容易知道这一点,你应该结合某种构建工具的知识,如 NAnt MSBuild FinalBuilder 等。这些工具提供了完整的构建环境,不仅仅是基本的编译器。

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.

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

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