【问题标题】:How do I get the path or directory of my currently-executing script in CSI?如何在 CSI 中获取当前正在执行的脚本的路径或目录?
【发布时间】:2017-10-18 15:49:26
【问题描述】:

从 CSI 脚本获取脚本路径的最佳方法是什么?我希望能够让我的脚本从在不同目录中运行的脚本中成为#loaded,并且仍然可以访问它自己的目录并且不受工作目录的影响。我找不到像 nodejs 的 __dirname 常量 in the docs 这样的东西。

【问题讨论】:

    标签: c# c#-interactive csi


    【解决方案1】:

    目前,我能找到找出当前正在执行的脚本目录的最佳方法是非常冗长。我想知道是否有一些我不知道的快捷方式/内置,但这里有:

    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()}");
    

    【讨论】:

      猜你喜欢
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 2022-07-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-10
      • 2010-10-01
      • 2012-05-04
      相关资源
      最近更新 更多