LINQPad script directory?
Asked Answered
R

3

56

Does anyone know how to get hold of the path to the directory where the LINQPad script file (.linq) resides?

Or to the script itself for that matter.

Note that I'm not talking about the location of the "My Queries" folder, the one shown inside LINQPad.

I can save a .linq file anywhere on disk, and open it by double-clicking on it.

For instance, if I save the file to C:\Temp\Test.linq, and execute the program, I'd like to have either C:\Temp or C:\Temp\Test.linq.

Basically I'd like something akin to Environment.CurrentDirectory or Assembly.GetEntryAssembly().Location, just for the .linq file.

Things I've tried:

  • Looking through environment variables
  • Looking through the LINQPad assembly that is given to my script
  • Throwing an exception and looking at the stacktrace (contains a link to a temporary copy of my script somewhere else)
  • Environment.CommandLine.Dump(); - gives LINQPad executable
  • Environment.CurrentDirectory.Dump(); - gives C:\windows\system32
  • Assembly.GetExecutingAssembly().Location.Dump(); - gives temp directory
Rosenbaum answered 27/9, 2010 at 10:4 Comment(2)
The execution context is always going to be the program location. You need a specific LinqPAD API if one exists for the file location.Ambassadress
Yes, I was kinda wondering if someone know what that was :) (assuming there is one)Rosenbaum
F
109

I've just added a feature to address this. You can test it now by downloading the 2.27.1 or 4.27.1 beta build.

To get the current query's folder, use the following expression:

Path.GetDirectoryName (Util.CurrentQueryPath)
Fabrizio answered 28/9, 2010 at 9:29 Comment(4)
There are many reasons why I just love LINQPad, and you seem to have no problem coming up with new reasons :) Thank you so much!Rosenbaum
How is it I only just found the Util type?!Stickup
Linqpad v5.x Util.CurrentQueryPath returns null.Nuristan
Have you saved your query? (If not, there will be no query path)Fabrizio
P
0

I presume you mean programatically and not through the UI itself.

The path to the linq file directory is held in

%APPDATA%\LINQPad\querypath.txt

Puffer answered 27/9, 2010 at 10:24 Comment(4)
No, that contains the path to the "My Queries" directory. I can easily save .linq files everywhere else, and it's the real location I want, I'll edit the question to clarify. I want the full path to the .linq file of the currently executing script.Rosenbaum
Ah ok - the question is clearer now. I don't know the answer but what would you like returned when you execute a query that hasn't yet been saved?Puffer
Anything basically, temp, whatever. It would probably in that case be logical that it returned the path to a temporary copy of the script. For instance, for some reason I might be interested in reading the source, but my primary use is to access data files in the same directory as the script itself, without hardcoding the path to where the script lives.Rosenbaum
I've posted the question on the forum of the LINQPad authors book, so I'll see if that can give me the specifics, or perhaps a feature request :)Rosenbaum
A
0

The next question after "How do I get LinqPad script directory" is "How do I set it?" below is how you do it and link to where I found the answer

Directory.SetCurrentDirectory (Path.GetDirectoryName (Util.CurrentQueryPath));
Alpine answered 8/4, 2022 at 21:34 Comment(2)
That changes the "current directory" of the process, it does not change the LINQPad script directory location.Rosenbaum
Good to know. Thanks for the clarification.Alpine

© 2022 - 2024 — McMap. All rights reserved.