如何执行C#文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/271438/
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 to execute C# file
提问by
I am having a .cs file, i need to execute that file.. I don't have experience in C#.. Please help.. Do we have to install any software to run it..
我有一个 .cs 文件,我需要执行该文件.. 我没有 C# 方面的经验.. 请帮忙.. 我们是否必须安装任何软件才能运行它..
Currently i am using windows XP..
目前我正在使用Windows XP ..
Please help me...
请帮我...
回答by thijs
Grab the .NET SDK(perhaps not needed, see comments) and see if you can compile your .cs file with CSC.exeAlternatively try Visual Studio Express Edition
获取.NET SDK(可能不需要,请参阅评论)并查看是否可以使用CSC.exe编译 .cs 文件 或者尝试 Visual Studio Express Edition
But if you don't have ANY experience with software engineering, this might not be the best way to start...
但是如果你没有任何软件工程经验,这可能不是最好的开始方式......
回答by CMS
Snippet Compileralso can be useful.
Snippet Compiler也很有用。
回答by chakrit
There is this C#-Scriptprojectthat enables you to run .cs files as a script (vbs-style)
有这个C#-Script项目,它使您能够将 .cs 文件作为脚本(vbs 样式)运行
It seemed to me that you just want to execute that single file, so C#-Script should do it without the need to learn any compilings.
在我看来,您只想执行该单个文件,因此 C#-Script 应该无需学习任何编译即可完成。
But anyway, you'd still need the .NET Framework. Grab that first.
但无论如何,您仍然需要 .NET Framework。先抓住那个。
回答by spinodal
.cs file is only a file that holds the code, it cannot be executed directly. You should compile/build it first (to executable format) with suitable tools.
.cs 文件只是一个保存代码的文件,不能直接执行。您应该首先使用合适的工具编译/构建它(到可执行格式)。
You can start somewhere like this: http://www.csharp-station.com/Tutorials/Lesson01.aspx
你可以从这样的地方开始:http: //www.csharp-station.com/Tutorials/Lesson01.aspx
回答by jan
A .cs file cannot be executed by itself. It needs to be compiled first.
.cs 文件不能单独执行。它需要先编译。
I'd suggest you download Visual C# 2008 Express, which is free..
我建议您下载免费的Visual C# 2008 Express。
And start reading
并开始阅读
回答by Xiaofu
Download and install the .NET framework from Microsoft, current version at the time of writing is v3.5 SP1 from here.
从此处下载并安装 Microsoft 的 .NET 框架,撰写本文时的当前版本为 v3.5 SP1 。
If you're going to do any serious developement, also download and install the .NET SDK v3.5 from here. This wil give you extra tools and development documentation.
如果您要进行任何认真的开发,还可以从此处下载并安装 .NET SDK v3.5 。这将为您提供额外的工具和开发文档。
Once you've installed this, it should create an "SDK Command Prompt" for your use. Open this, and navigate to the directory containing your .cs file. Assuming this single .cs file is a complete application that doesn't depend on any other assemblies (like DLLs), you can now compile it. Enter the following in your command prompt (replace the filenames with relevant info):
安装后,它应该会创建一个“SDK 命令提示符”供您使用。打开它,并导航到包含 .cs 文件的目录。假设这个单一的 .cs 文件是一个不依赖于任何其他程序集(如 DLL)的完整应用程序,您现在可以编译它。在命令提示符中输入以下内容(用相关信息替换文件名):
csc /out:your_app_name.exe your_cs_filename.cs
If all is well with your .cs file, you should now have a working .exe that you can run. If not, you may be back on StackOverflow for debugging advice...
如果您的 .cs 文件一切正常,您现在应该有一个可以运行的有效 .exe。如果没有,您可能会回到 StackOverflow 寻求调试建议...
回答by rohancragg
If you want to do windows scripting in a .NET language similar to C# you could also have a look at Windows PowerShell.
如果您想使用类似于 C# 的 .NET 语言编写 Windows 脚本,您还可以查看 Windows PowerShell。
As everyone here has already mentioned - in any event you will need a version of the Microsoft .NET framework to be installed first.
正如这里的每个人都已经提到的那样 - 在任何情况下,您都需要先安装 Microsoft .NET 框架的一个版本。
回答by huncyrus
Use any c# compiler, but the best way is the ms visual studio (or studio express) ms visual studio here
使用任何 c# 编译器,但最好的方法是 ms Visual Studio (or studio express) ms visual studio here
回答by NanoWar
If you have dotnet installed (comes with VS) you can try this batch script, which will execute any .net core cs-file that has a static Main() function.
如果您安装了 dotnet(VS 附带),您可以尝试这个批处理脚本,它将执行任何具有静态 Main() 函数的 .net 核心 cs 文件。
Usage is "cs [file.cs]"
用法是“cs [file.cs]”
:-)
:-)
@echo off
IF "%1"=="" GOTO HELP
REM => copy .net core project file into temp folder
mkdir _temp 2>NUL
echo ^<Project Sdk="Microsoft.NET.Sdk.Web"^>^<PropertyGroup^>^<TargetFramework^>netcoreapp2.0^</TargetFramework^>^</PropertyGroup^>^</Project^> > _temp\p.csproj
copy %1 _temp 1>NUL
cd _temp
REM => run program
dotnet run
REM => clean-up
cd ..
rmdir /Q /S _temp 1>NUL
GOTO EXIT
:HELP
echo Runs any C# file with a static Main() directly
echo Usage: %0 [file.cs]
:EXIT