silent_coder
silent_coder

Reputation: 6522

run single *.cs script from command line

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?

Upvotes: 46

Views: 44344

Answers (5)

Alex from Jitbit
Alex from Jitbit

Reputation: 60712

PowerShell one liner out of the box

(Add-Type -Path "Program.cs" -PassThru)::Main()

P.S. In your particular case the Main() method has args parameter and it will complain that it's missing. In that case call Main($null)

Upvotes: 13

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

Upvotes: 34

Chtioui Malek
Chtioui Malek

Reputation: 11515

For this i've always used this c# scripting engine : http://csscript.net,

very easy to implement and works very well and you can reference local dlls, Nuget packages or GAC assemblies.

Upvotes: 2

JensG
JensG

Reputation: 13421

It can be done using Powershell. Assumed, your code is in a file test.cs in the current folder:

$source = (Get-Content .\test.cs) -join " "
Add-Type $source -Language CSharp  
[Scripts.Program]::Main((""))

gives

PS> .\test.ps1
This is the miracle

So how could I execute the code in single file using command line in relatively easy manner?

Wrap the above code into a function, make the file name an parameter, put that function in your Powershell profile and run it whenever you want. But be aware of the fact, that as soon as you need other assemblies they must be specified when doing the call. Here's a slightly more elaborat example.

Upvotes: 5

Lee Gunn
Lee Gunn

Reputation: 8656

Pretty sure you'll need a project.json file. Here's a bare bones file to get it running:

{
  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.0-*",
      "type": "platform"
    }
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": {}
    }
  },

  "buildOptions": {
    "emitEntryPoint": true
  }
}

Note the emitEntryPoint.

I had to dotnet restore first and then dotnet run test.cs.

Upvotes: 1

Related Questions