Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relative directory paths in Visual Studio using FSharp

I have a file located in a solution directory like this:

enter image description here

I want to read the contents of one of the .txt files into a string in the FSI:

open System.IO

[<Literal>]
let path = "../Data/Build_Keynote2014.txt"

let buildKeynote =  File.ReadAllText(path)

The problem is that it is throwing an exception:

System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Users\jamie\AppData\Local\Data\Build_Keynote2014.txt'.

Is there a way to reference the file without using the full path?

Thanks

like image 418
Jamie Dixon Avatar asked May 23 '15 18:05

Jamie Dixon


1 Answers

After some research, I found this post

let baseDirectory = __SOURCE_DIRECTORY__
let baseDirectory' = Directory.GetParent(baseDirectory)
let filePath = "Data\Build_Keynote2014.txt"
let fullPath = Path.Combine(baseDirectory'.FullName, filePath)
let buildKeynote =  File.ReadAllText(fullPath)

works like a charm. Thanks everyone who submitted.

like image 125
Jamie Dixon Avatar answered Nov 03 '22 18:11

Jamie Dixon