1
從CSI腳本中獲取腳本路徑的最佳方法是什麼?我希望能夠通過運行在不同目錄中的腳本來編輯我的腳本,並且仍然可以訪問其自己的目錄,而不受工作目錄的影響。我找不到像nodejs的__dirname
常量in the docs。如何在CSI中獲取當前正在執行的腳本的路徑或目錄?
從CSI腳本中獲取腳本路徑的最佳方法是什麼?我希望能夠通過運行在不同目錄中的腳本來編輯我的腳本,並且仍然可以訪問其自己的目錄,而不受工作目錄的影響。我找不到像nodejs的__dirname
常量in the docs。如何在CSI中獲取當前正在執行的腳本的路徑或目錄?
目前,找出當前正在執行的腳本目錄的最佳方式是非常冗長的。我不知道是否有一些快捷方式/內置的,我不知道,但這裏有雲:
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()}");