Search code examples
.netscriptingf#.net-5

How to know if .fsx script being run by .NETCore/.NET5 or by legacy framework?


My .fsx scripts are kind of old so they only work with .NET4.x, which means I need to run them with the fsharpi command in macOS.

Now I'm thinking to start migrating them to dotnet fsi; however, ideally I can find a way to run them in both ways for a while, in a transition period.

So I was hoping that, inside them, I could do some kind of conditional code like this:

#if NET5_0
System.Console.WriteLine "we're running in dotnet fsi, yay"
#else
System.Console.WriteLine "we're running with fsharpi, legacy"
#endif

But it doesn't work, surprisingly (and I say surprisingly because the define NET5_0 is being mentioned in many documentation pages). Any other idea of how I could detect this?


Solution

  • I think System.Environment.Version will give you the info you need:

    if System.Environment.Version.Major >= 5 then
        System.Console.WriteLine "we're running in dotnet fsi, yay"
    else
        System.Console.WriteLine "we're running with fsharpi, legacy"
    

    It looks like NET5_0 is only defined for C#.