gpt4 book ai didi

C# 用于脚本文件的脚本 (csx) 位置

转载 作者:太空狗 更新时间:2023-10-30 01:30:14 26 4
gpt4 key购买 nike

在 F# 中,使用预定义标识符 __SOURCE_DIRECTORY__ 相当容易 https://stackoverflow.com/a/4861029/2583080

但是,此标识符在 C# 脚本(csx 文件或 C# Interactive)中不起作用。

> __SOURCE_DIRECTORY__

(1,1): error CS0103: The name '__SOURCE_DIRECTORY__' does not exist in the current context

以更传统的方式获取当前目录也行不通。

  1. Directory.GetCurrentDirectory()

    返回:C:\Users\$USER_NAME$\

  2. 新的 Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath;

    返回:C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\CommonExtensions\Microsoft\ManagedLanguages\VBCSharp\InteractiveComponents\

最佳答案

在 C# 中,您可以利用 caller information属性(自 C# 5/VS2012 起可用)。只需像这样声明一个方法:

string GetCurrentFileName([System.Runtime.CompilerServices.CallerFilePath] string fileName = null)
{
return fileName;
}

并在不指定可选参数的情况下调用它:

string scriptPath = GetCurrentFileName(); // /path/to/your/script.csx

关于C# 用于脚本文件的脚本 (csx) 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46728845/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com