Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获取正在执行的.csx脚本的路径?_C#_Csx_Csi - Fatal编程技术网

C# 如何获取正在执行的.csx脚本的路径?

C# 如何获取正在执行的.csx脚本的路径?,c#,csx,csi,C#,Csx,Csi,使用C:\ProgramFiles(x86)\MSBuild\14.0\Bin\amd64\csi.exe执行C#脚本(.csx)时,如何获取正在执行的脚本的路径 线路 Console.WriteLine(AppDomain.CurrentDomain.BaseDirectory); 打印解释器的路径(csi.exe),而不是脚本 脚本应该在某种程度上知道它的路径,因为您可以使用如下相对路径加载程序集: #r "..\\bla\\asdf.dll" 只有使用helper方法,我才能通过编程

使用
C:\ProgramFiles(x86)\MSBuild\14.0\Bin\amd64\csi.exe执行C#脚本(
.csx
)时,如何获取正在执行的脚本的路径

线路

Console.WriteLine(AppDomain.CurrentDomain.BaseDirectory); 
打印解释器的路径(
csi.exe
),而不是脚本

脚本应该在某种程度上知道它的路径,因为您可以使用如下相对路径加载程序集:

#r "..\\bla\\asdf.dll"

只有使用helper方法,我才能通过编程获得.CSX源脚本的完整路径。该方法使用属性。这可以从交互式Rosalyn C#shell以及命令行内部工作

using System.Runtime.CompilerServices;
// Work-around helper method to get the source file location.
private static string GetSourceFile([CallerFilePath] string file = "") => file;
Console.WriteLine($"Full path to .csx source file => {GetSourceFile()}");

必须指定脚本文件位置的完整路径。如果未指定完整路径,VS将默认为项目的bin文件夹。