这是场景:

  • 打开Visual Studio。这是在VS2010 Pro中完成的。
  • 在Visual Studio中打开F#Interactive
  • 使用fsx文件打开项目
    注意:项目和fsx文件位于E:\<directories>\fsharp-tapl\arith
  • 从fsx文件向F#Interactive发送命令
    > System.Environment.CurrentDirectory;;
    val it : string = "C:\Users\Eric\AppData\Local\Temp"
    

    我没想到会有Temp目录,但这是有道理的。
    > #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的位置。
    > #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
    > #r @"arith.exe"
    --> Referenced 'E:\<directories>\fsharp-tapl\arith\bin\Debug\arith.exe'
    

    F#Interactive会正确找到arith.exe,报告正确的绝对路径。
    > open Main
    > eval "true;" ;;
    true
    val it : unit = ()
    

    这确认arith.exe已正确找到,加载并正常运行。

  • 那么由于当前目录没有帮助,F#Interactive #I命令如何知道项目路径?

    我真正要追求的是从F#Interactive内部获得如何到达项目的路径E:\<directories>\fsharp-tapl\arith

    编辑
    > printfn __SOURCE_DIRECTORY__;;
    E:\<directories>\fsharp-tapl\arith
    val it : unit = ()
    

    最佳答案

    在F#Interactive中,要搜索的默认目录是源目录。您可以使用__SOURCE_DIRECTORY__轻松查询它。

    此行为非常方便,允许您使用相对路径。您通常将fsx文件与fs文件放在同一文件夹中。

    #load "Ast.fs"
    #load "Core.fs"
    

    当您引用相对路径时,F#Interactive将始终使用隐式源目录作为起点。
    #I ".."
    #r ... // Reference some dll in parent folder of source directory
    #I ".."
    #r ... // Reference some dll in that folder again
    

    如果您想记住旧目录以备下次引用,则应改用#cd:
    #cd "bin"
    #r ... // Reference some dll in bin
    #cd "Debug"
    #r ... // Reference some dll in bin/Debug
    

    关于visual-studio - F#Interactive #I命令如何知道项目路径?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14673544/

    10-09 01:55