Is it possible to use an app.config file from a F# script (fsx) file? If so, how? Where do I put the file and what will it be called?
Asked
Active
Viewed 1,145 times
7
-
4Already answered here: http://stackoverflow.com/questions/645622/app-config-and-f-interactive-not-working – Joe B. Dec 11 '12 at 04:44
-
Alternatively here if you have a heavy use of ConfigurationManager in referenced libraries: https://stackoverflow.com/questions/6150644/change-default-app-config-at-runtime/6151688#6151688 – Lamarth Sep 01 '20 at 14:08
1 Answers
0
The linked question by @JoeB provides 2 answers which are more suited to F# Interactive than .fsx scripts. I provide a good solution for F# scripts below:
Given this app.config file:
<configuration>
<appSettings>
<add key="foo" value="bar"/>
</appSettings>
</configuration>
Read foo
this way:
let appConfigPath = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "app.config")
let fileMap = ExeConfigurationFileMap()
fileMap.ExeConfigFilename <- appConfigPath
let config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None)
let foo = config.AppSettings.Settings.["foo"].Value
Console.WriteLine(foo)

knocte
- 16,941
- 11
- 79
- 125