2

When executing a C# script (.csx) with C:\Program Files (x86)\MSBuild\14.0\Bin\amd64\csi.exe, how do I get the path to the script being executed?

The line

Console.WriteLine(AppDomain.CurrentDomain.BaseDirectory); 

prints the path to the interpreter (csi.exe), not the script.

The script should be aware of it's path at some level, as you can load assemblies with a relative path like this:

#r "..\\bla\\asdf.dll"
chtenb
  • 14,924
  • 14
  • 78
  • 116
  • You must specify the full path of the location of the script file. VS will default to the bin folder of the project if full path is not specified. – jdweng Jan 08 '18 at 12:31

1 Answers1

2

Only by using a helper method, could I programatically obtain the full path to the .CSX source script. The method uses the CallerFilePath attribute. This works from inside the interactive Rosalyn C# shell, as well as command line.

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()}");
Jordan
  • 453
  • 4
  • 6