问题描述
这是场景:
- 打开Visual Studio.这是在VS2010 Pro中完成的.
- 在Visual Studio中打开F#Interactive
- 使用fsx文件打开项目
注意:Project和fsx文件位于E:\<directories>\fsharp-tapl\arith
中 -
从fsx文件向F#Interactive发送命令
- Open Visual Studio. This was done in VS2010 Pro.
- Open F# Interactive within Visual Studio
- Open project with fsx file
Note: Project and fsx file are inE:\<directories>\fsharp-tapl\arith
Send commands to F# Interactive from fsx file
> System.Environment.CurrentDirectory;;
val it : string = "C:\Users\Eric\AppData\Local\Temp"
我不希望有Temp目录,但这很有意义.
I was not expecting a Temp directory but it makes sense.
> #r @"arith.exe"
Examples.fsx(7,1): error FS0082: Could not resolve this reference.
Could not locate the assembly "arith.exe".
Check to make sure the assembly exists on disk.
If this reference is required by your code, you may get compilation errors.
(Code=MSB3245)
Examples.fsx(7,1): error FS0084: Assembly reference 'arith.exe' was not found
or is invalid
#r命令错误表明F#Interactive当前不知道arith.exe的位置.
The #r command error shows that F# Interactive currently does not know the location of arith.exe.
> #I @"bin\Debug"
--> Added 'E:\<directories>\fsharp-tapl\arith\bin\Debug'
to library include path
因此,我们告诉F#Interactive arith.exe的位置.请注意,该路径不是绝对路径,而是项目的子路径.我还没有告诉F#Interactive arith项目的位置E:\<directories>\fsharp-tapl\arith
So we tell F# Interactive the location of the arith.exe.Notice that the path is NOT an absolute path but a sub-path of the project.I have not told F# Interactive the location of the arith projectE:\<directories>\fsharp-tapl\arith
> #r @"arith.exe"
--> Referenced 'E:\<directories>\fsharp-tapl\arith\bin\Debug\arith.exe'
F#Interactive会正确找到arith.exe报告正确的绝对路径.
And F# Interactive correctly finds arith.exe reporting the correct absolute path.
> open Main
> eval "true;" ;;
true
val it : unit = ()
这确认arith.exe已正确找到,加载并正常工作.
This confirms that arith.exe was correctly found, loaded and works.
由于当前目录没有帮助,所以F#Interactive #I命令如何知道项目路径?
So how did F# Interactive #I command know the project path since the current directory is of no help?
我真正追求的是来自F#Interactive内部的人如何获得项目E:\<directories>\fsharp-tapl\arith
的路径.
What I am really after is from within F# Interactive how does one get the path to the project, E:\<directories>\fsharp-tapl\arith
.
编辑
> printfn __SOURCE_DIRECTORY__;;
E:\<directories>\fsharp-tapl\arith
val it : unit = ()
推荐答案
在F#Interactive中,要搜索的默认目录是源目录.您可以使用__SOURCE_DIRECTORY__
轻松查询它.
In F# Interactive, the default directory to search is the source directory. You can query it easily using __SOURCE_DIRECTORY__
.
此行为非常方便,允许您使用相对路径.通常,fsx
文件与fs
文件位于同一文件夹中.
This behaviour is very convenient to allow you to use relative paths. You often have fsx
files in the same folder with fs
files.
#load "Ast.fs"
#load "Core.fs"
当您引用相对路径时,F#Interactive将始终使用隐式源目录作为起点.
When your refer to a relative path, F# Interactive will always use the implicit source directory as the starting point.
#I ".."
#r ... // Reference some dll in parent folder of source directory
#I ".."
#r ... // Reference some dll in that folder again
如果您想记住旧目录以备下次参考,则应改用#cd
:
If you want to remember the old directory for next reference, you should use #cd
instead:
#cd "bin"
#r ... // Reference some dll in bin
#cd "Debug"
#r ... // Reference some dll in bin/Debug
这篇关于F#Interactive #I命令如何知道项目路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!