F#FSI,更改工作目录 [英] F# FSI, Change working directory

查看:66
本文介绍了F#FSI,更改工作目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在桌面的新文件夹"中有以下文件:

I have the following file located in a "New Folder" of Desktop:

// File location: "C:\Users\my_user_name\Desktop\New folder\AddOne.fs"
// 
module internal AddOneModule

let AddOneFunction x = x + 1

我可以使用FSI F#Interactive在完整路径名上使用#load来访问此文件.

I can access this file by using #load on the full path name using FSI F# Interactive.

Microsoft (R) F# Interactive version 4.1
Copyright (c) Microsoft Corporation. All Rights Reserved.

For help type #help;;

> #load "C:\Users\my_user_name\Desktop\New folder\AddOne.fs";;
//[Loading C:\Users\my_user_name\Desktop\New folder\AddOne.fs]
//namespace FSI_0002
//  val AddOneFunction : x:int -> int

> open AddOneModule;;
> AddOneFunction 100;;
//  val it : int = 101

如何更改工作目录,以便可以使用相对路径访问文件?

How do I change the working directory so that I can access the file using relative path?

F#交互式:如何显示/更改当前工作目录

我尝试了与上面的帖子类似的方法,但是FSI仍然尝试在Temp文件夹中查找文件:

I tried something similar to the post above, but FSI still tries to find the file in the Temp folder:

(RESET FSI)

(RESET FSI)

Microsoft (R) F# Interactive version 4.1
Copyright (c) Microsoft Corporation. All Rights Reserved.

For help type #help;;

> open System;;
> Environment.CurrentDirectory <- @"C:\Users\my_user_name\Desktop\New folder";;
//val it : unit = ()

> #load "AddOne.fs";;

//  #load "AddOne.fs";;
//  ^^^^^^^^^^^^^^^^^

//C:\Users\my_user_name\Desktop\New folder\stdin(3,1): error FS0078: Unable to find 
//the file 'AddOne.fs' in any of
// C:\Users\my_user_name\AppData\Local\Temp

谢谢您的帮助.

推荐答案

您可以使用

Instead of changing the working directory you may achieve the desired using a trick with __SOURCE_DIRECTORY__ built-in identifier.

首先,您需要目录结构中的某个定位点.为了便于说明,我们假设您在Windows上,并将此定位点作为用户目录,该目录由%USERPROFILE%环境变量定义.将包含以下单行代码的脚本anchorfsi.fsx放在此处:

To begin with, you need a certain anchor point in directory structure. For illustration purposes let's assume you are on Windows and let this anchor point be your user directory, which is defined by %USERPROFILE% environment variable. Place there a script anchorfsi.fsx containing the following single line of code:

#I __SOURCE_DIRECTORY__

基本上,这就是您需要做的.现在,无论您使用命令行fsi --load:%USERPROFILE%\anchorfsi.fsx从哪个位置射击fsi,都可以在脚本和交互式命令中使用相对路径.

That's basically all you need to do. Now, regardless from what location you shoot your fsi using command line fsi --load:%USERPROFILE%\anchorfsi.fsx, you can use relative paths in your scripts and in interactive commands.

在加载问题.\desktop\new folder\addone.fs时转到问题中的设置,以下屏幕截图演示了如何实现所需的目标:

Turning to the setup in your question with loading .\desktop\new folder\addone.fs, the following screenshot demonstrates achieving the desired:

请注意如何将输入的相对路径".\desktop\new folder\addone.fs"正确地映射到绝对C:\Users\gene\desktop\new folder\addone.fs 而不依赖于fsi工作目录的任何上.

Notice how the entered relative path ".\desktop\new folder\addone.fs" was correctly mapped to the absolute one C:\Users\gene\desktop\new folder\addone.fs without any dependency upon the fsi working directory whatsoever.

这篇关于F#FSI,更改工作目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆