如何在不使用 Visual Studio 的情况下编译和运行 c# 程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18286855/
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
How can I compile and run c# program without using visual studio?
提问by A.s. Bhullar
I am very new to C#. I have just run C# 'Hello World' program using Visual Studio.
我对 C# 很陌生。我刚刚使用 Visual Studio 运行了 C#“Hello World”程序。
Can I run or compile a C# program without using Visual Studio?
我可以在不使用 Visual Studio 的情况下运行或编译 C# 程序吗?
If it is possible, then which compiler should I use?
如果可能,那么我应该使用哪个编译器?
Thanks
谢谢
采纳答案by xanatos
If you have .NET v4 installed (so if you have a newer windows or if you apply the windows updates)
如果您安装了 .NET v4(因此如果您有更新的 Windows 或应用 Windows 更新)
C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe somefile.cs
or
或者
C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe nomefile.sln
or
或者
C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe nomefile.csproj
It's highly probable that if you have .NET installed, the %FrameworkDir%
variable is set, so:
如果您安装了 .NET,很可能%FrameworkDir%
会设置该变量,因此:
%FrameworkDir%\v4.0.30319\csc.exe ...
%FrameworkDir%\v4.0.30319\msbuild.exe ...
回答by Daniel Mo?mondor
You can use .NET sdk, or alternatively:
您可以使用 .NET sdk,或者:
Online compiler doesn't offer much, but can be learning tool for simple homework-style tasks.
在线编译器提供的内容不多,但可以作为简单家庭作业式任务的学习工具。
回答by Elvin Mammadov
There are different ways for this:
有不同的方法:
1.Building C# Applications Using csc.exe
1.使用 csc.exe 构建 C# 应用程序
While it is true that you might never decide to build a large-scale application using nothing but the C# command-line compiler, it is important to understand the basics of how to compile your code files by hand.
虽然您可能永远不会决定只使用 C# 命令行编译器来构建大型应用程序,但了解如何手动编译代码文件的基础知识很重要。
2.Building .NET Applications Using Notepad++
2.使用 Notepad++ 构建 .NET 应用程序
Another simple text editor I'd like to quickly point out is the freely downloadable Notepad++ application. This tool can be obtained from http://notepad-plus.sourceforge.net. Unlike the primitive Windows Notepad application, Notepad++ allows you to author code in a variety of languages and supports
我想快速指出的另一个简单的文本编辑器是可免费下载的 Notepad++ 应用程序。这个工具可以从http://notepad-plus.sourceforge.net获得。与原始的 Windows 记事本应用程序不同,Notepad++ 允许您以多种语言编写代码并支持
3.Building .NET Applications Using SharpDevelop
3.使用 SharpDevelop 构建 .NET 应用程序
As you might agree, authoring C# code with Notepad++ is a step in the right direction, compared to Notepad. However, these tools do not provide rich IntelliSense capabilities for C# code, designers for building graphical user interfaces, project templates, or database manipulation utilities. To address such needs, allow me to introduce the next .NET development option: SharpDevelop (also known as "#Develop").You can download it from http://www.sharpdevelop.com.
您可能同意,与记事本相比,使用 Notepad++ 编写 C# 代码是朝着正确方向迈出的一步。但是,这些工具没有为 C# 代码、用于构建图形用户界面的设计器、项目模板或数据库操作实用程序提供丰富的 IntelliSense 功能。为了满足这些需求,请允许我介绍下一个 .NET 开发选项:SharpDevelop(也称为“#Develop”)。您可以从http://www.sharpdevelop.com下载它 。
回答by cecilphillip
Another option is an interesting open source project called ScriptCS. It uses some crafty techniques to allow you a development experience outside of Visual Studio while still being able to leverage NuGet to manage your dependencies. It's free, very easy to install using Chocolatey. You can check it out here http://scriptcs.net.
另一种选择是一个有趣的开源项目,称为 ScriptCS。它使用一些巧妙的技术让你在 Visual Studio 之外获得开发体验,同时仍然能够利用 NuGet 来管理你的依赖项。它是免费的,使用Chocolatey很容易安装。你可以在这里查看它http://scriptcs.net。
Another cool feature it has is the REPL from the command line. Which allows you to do stuff like this:
它的另一个很酷的功能是来自命令行的 REPL。这允许你做这样的事情:
C:\> scriptcs
scriptcs (ctrl-c or blank to exit)
> var message = "Hello, world!";
> Console.WriteLine(message);
Hello, world!
>
C:\>
You can create C# utility "scripts" which can be anything from small system tasks, to unit tests, to full on Web APIs. In the latest release I believe they're also allowing for hosting the runtime in your own apps.
您可以创建 C# 实用程序“脚本”,它可以是任何内容,从小型系统任务到单元测试,再到完整的 Web API。在最新版本中,我相信他们还允许在您自己的应用程序中托管运行时。
Check out it development on the GitHub page too https://github.com/scriptcs/scriptcs
在 GitHub 页面上也查看它的开发https://github.com/scriptcs/scriptcs
回答by Ramakant
If you have a project ready and just want to change some code and then build. Check out MSBuild which is located in the Microsoft.Net under windows directory.
如果您已经准备好一个项目并且只想更改一些代码然后构建。查看位于 Windows 目录下 Microsoft.Net 中的 MSBuild。
C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild "C:\Projects\MyProject.csproj" /p:Configuration=Debug;DeployOnBuild=True;PackageAsSingleFile=False;outdir=C:\Projects\MyProjects\Publish\ (Please do not edit, leave as a single line)
C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild "C:\Projects\MyProject.csproj" /p:Configuration=Debug;DeployOnBuild=True;PackageAsSingleFile=False;outdir=C:\Projects\MyProjects\发布\(请勿编辑,单行保留)
... The line above broken up for readability
... 上面的行为了可读性而分开
C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild "C:\Projects\MyProject.csproj" /p:Configuration=Debug;DeployOnBuild=True;PackageAsSingleFile=False; outdir=C:\Projects\MyProjects\Publish\
C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild "C:\Projects\MyProject.csproj" /p:Configuration=Debug;DeployOnBuild=True;PackageAsSingleFile=False; outdir=C:\Projects\MyProjects\Publish\
回答by Big McLargeHuge
I use a batch script to compile and run C#:
我使用批处理脚本来编译和运行 C#:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc /out:%1 %2
@echo off
if errorlevel 1 (
pause
exit
)
start %1 %1
I call it like this:
我这样称呼它:
C:\bin\csc.bat "C:\code\MyProgram.exe" "C:\code\MyProgram.cs"
I also have a shortcut in Notepad++, which you can define by going to Run > Run...:
我在 Notepad++ 中也有一个快捷方式,您可以通过运行 > 运行...来定义:
C:\bin\csc.bat "$(CURRENT_DIRECTORY)$(NAME_PART).exe" "$(FULL_CURRENT_PATH)"
I assigned this shortcut to my F5 key for maximum laziness.
我将此快捷方式分配给了我的 F5 键,以最大限度地保持懒惰。