run single *.cs script from command line

17,135

Solution 1

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

Solution 2

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.

Solution 3

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)

Solution 4

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.

Solution 5

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.

Share:
17,135

Related videos on Youtube

silent_coder
Author by

silent_coder

Updated on August 23, 2021

Comments

  • silent_coder
    silent_coder over 2 years

    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?

  • silent_coder
    silent_coder almost 8 years
    so this options is only for projects and don't allow run single scripts, right?
  • Lee Gunn
    Lee Gunn almost 8 years
    I think so yeah, but if someone could confirm that would be good. I notice if you do a "dotnet new" it creates two files. A "Hello World" Program.cs and a simple project.json file.
  • benrwb
    benrwb over 4 years
    Note that using -join " " will put the entire contents of the program onto one line. If the program contains a comment // then the rest of the program will be commented out. To avoid this, use -join "`r`n" instead.
  • Nick Cox
    Nick Cox about 2 years
    @benrwb or you could use Get-Content -raw and it won't split the content into lines in the first place.