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文件夹。