0

What is the best way to get my script’s path from a CSI script? I want to be able to have my script be #loaded from a script running in a different directory and still get access to its own directory and not be affected by the working directory. I can’t find anything like nodejs’s __dirname constant in the docs.

binki
  • 7,754
  • 5
  • 64
  • 110

1 Answers1

1

Currently, the best way I can find to figure out the currently executing script’s directory is quite verbose. I wonder if there is some shortcut/built-in that I do not know about, but here goes:

GetMineDirectory.csx:

#!/usr/bin/env csi

using System;
using System.IO;
using System.Runtime.CompilerServices;

static string GetMyPath(
    [CallerFilePath] string path = "")
    => path;

// Do not write in terms of GetMyPath() in case we are called from another script.
static string GetMyDirectory(
    [CallerFilePath] string path = "")
    => Path.GetDirectoryName(path);

Console.WriteLine($"My full path is {GetMyPath()}");

Console.WriteLine($"My directory is {GetMyDirectory()}");
binki
  • 7,754
  • 5
  • 64
  • 110