Is there at last a easy way to execute c# script file from command line?
I saw that discussion on github
and according to this thread i think dotnet run Test.cs
should do the job.
But for my testclass which is:
using System; namespace Scripts { public class Program { public static void Main(string[] args) { Console.Out.WriteLine("This is the miracle"); } } }
it fails
PM> dotnet run .\Test.cs dotnet.exe : Object reference not set to an instance of an object.At line:1 char:1
So how could I execute the code in single file using command line in relatively easy manner?
UPD 1: As correctly mentioned by @Lee and @svick dotnet run
is for running project. But my initial question was - how to run single file. Maybe some options using roslyn
?
You can invoke the C# compiler by typing the name of its executable file (csc.exe) at a command prompt. For . NET Framework projects, you can also run csc.exe from the command line.
To run C# Code in cmdset path of cmd by using (set path="") command. Now go to the folder (by using cmd) in which you save your c# file which you create in step 2. Now enter csc "your file name."cs in cmd. Now if you set correct path and your coding is correct then a .exe file is created run that file on cmd.
cs file is normally C# source code. You can open it in any text editor. If you want to actually run the program defined by the source code, you will need to compile it, for example by loading it into Visual Studio (the real Visual Studio, not Visual Studio Code) and building it and running it.
I found another solution on Scott Hanselman's blog:
https://www.hanselman.com/blog/CAndNETCoreScriptingWithTheDotnetscriptGlobalTool.aspx
It relies on a .NET CLI tool called dotnet-script
, you can find its repository below:
https://github.com/filipw/dotnet-script
To use, it, first install it using dotnet tool install -g dotnet-script
Then you can run dotnet script
and use it as a REPL or run dotnet script file.csx
to run a file.
To include a NuGet package reference, use #r "nuget: AutoMapper, 6.1.0"
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With