36

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
  • 6,222
  • 14
  • 47
  • 91
  • check this [link](https://msdn.microsoft.com/en-us/library/k1sx6ed2.aspx?f=255&MSPPError=-2147217396) it might help you. – MANISH KUMAR CHOUDHARY May 30 '16 at 11:11
  • 1
    That link is very old. [Here is the current documentation for `dotnet run`.](http://dotnet.github.io/docs/core-concepts/core-sdk/cli/dotnet-run.html) – svick May 30 '16 at 12:59
  • 1
    @MANISH That's not very relevant to `dotnet` CLI. – svick May 30 '16 at 13:01
  • There now exists the concept of a C# script - see the following question: https://stackoverflow.com/questions/25405941/what-are-csx-c-sharp-files-for. Conceptually, this should get you what you want. – ryanwebjackson Jan 04 '19 at 19:48
  • See also [How to compile and run a single class file cs file?](https://stackoverflow.com/questions/8712125/how-to-compile-and-run-a-single-class-file-cs-file) – Theraot Dec 23 '22 at 04:51

5 Answers5

23

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

Tomáš Hübelbauer
  • 9,179
  • 14
  • 63
  • 125
11

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)

Alex from Jitbit
  • 53,710
  • 19
  • 160
  • 149
5

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.

JensG
  • 13,148
  • 4
  • 45
  • 55
  • 2
    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. – benrwb Dec 04 '19 at 17:55
  • 1
    @benrwb or you could use `Get-Content -raw` and it won't split the content into lines in the first place. – Nick Cox Mar 03 '22 at 21:01
2

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.

Chtioui Malek
  • 11,197
  • 1
  • 72
  • 69
1

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.

Lee Gunn
  • 8,417
  • 4
  • 38
  • 33
  • so this options is only for projects and don't allow run single scripts, right? – silent_coder May 30 '16 at 12:07
  • 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. – Lee Gunn May 30 '16 at 15:25