简体   繁体   中英

how to execute C# file

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

Currently i am using windows XP..

Please help me...

Grab the .NET SDK (perhaps not needed, see comments) and see if you can compile your .cs file with CSC.exe Alternatively try Visual Studio Express Edition

But if you don't have ANY experience with software engineering, this might not be the best way to start...

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

You can start somewhere like this: http://www.csharp-station.com/Tutorials/Lesson01.aspx

There is this C#-Script project that enables you to run .cs files as a script (vbs-style)

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.

But anyway, you'd still need the .NET Framework. Grab that first.

A .cs file cannot be executed by itself. It needs to be compiled first.

I'd suggest you download Visual C# 2008 Express , which is free..

And start reading

Snippet Compiler也很有用。

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.

Usage is "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

使用任何 c# 编译器,但最好的方法是 ms Visual Studio (or studio express) ms visual studio here

Download and install the .NET framework from Microsoft, current version at the time of writing is v3.5 SP1 from here .

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.

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):

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

If you want to do windows scripting in a .NET language similar to C# you could also have a look at 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.

Visual Studio 2019 has "Execute in Interactive". Highlight code then right click, select "Execute in Interactive". Then you can write script code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM